【问题标题】:Displaying popup with flask and jquery使用烧瓶和 jquery 显示弹出窗口
【发布时间】:2019-04-22 00:38:54
【问题描述】:

在我的网站上单击按钮后,我想显示允许用户添加意见的新表单。我不确定应该以哪种方式将 jquery 脚本添加到我的项目中。我在简单的 html 网站上尝试过它并且它可以工作,但我不能对烧瓶应用程序做同样的事情。这是我的代码:

% extends "bootstrap/base.html" %}

{% block content %}

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/redmond/jquery-ui.css">
<script src="http://ajax.googleapis.com ajax/libs/jquery/1/jquery.js"></script>

 <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.js"></script>
 <script type="text/javascript">

$(function () {
  $("#btnclick").click(function () {
    $("#divpopup").dialog({
      title:"Dodaj opinie",
      width: 430,
      height: 200,
      modal:true,
      buttons: {
        Close:
        function(){
          $(this).dialog('close');
        }
      }
    });

  });
})

<div id="divpopup" style="display: none">
Here will be form
</div>

 <button type="button" id="btnclick">Add opinion</button>

{% endblock %}

点击按钮后什么也没发生。我用的是bootstrap和jinja2,要不要用其他的内容来添加脚本?

【问题讨论】:

    标签: javascript jquery twitter-bootstrap jquery-ui


    【解决方案1】:

    您可能会考虑为此使用 Bootstrap Modal,因为您说您在项目中使用 Bootstrap。

    这是从他们的网站复制的一个非常简单的示例:

    <!-- Button trigger modal -->
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
      Launch demo modal
    </button>
    
    <!-- Modal -->
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            INSERT FORM HERE
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>
    

    如果您使用的是 Bootstrap 4,您应该能够复制此权限并更改 IDs 以匹配您想要的。 文档链接:https://getbootstrap.com/docs/4.0/components/modal/

    【讨论】:

    • 好的,它有效。现在我必须寻找 soma AJAX 魔术来将信息发送到数据库
    • 很高兴你让它工作了。如果您在烧瓶应用程序中设置一条路线,将信息插入数据库,那应该非常容易。此链接包含有关执行 AJAX 帖子的信息:stackoverflow.com/questions/1960240/jquery-ajax-submit-form
    • 还要在 Flask 中实现路径,请查看:stackoverflow.com/questions/11556958/… 从那里您只需使用某种 SQL 库将其插入数据库。
    • 是的,我的应用程序在 atm 状态良好,所以我已经登录并向数据库添加了一些东西。问题是现在我想添加关于书籍的意见,所以我需要书籍的 ID,并且我不想在提交意见后刷新我的网站
    • 不确定您所说的“不知从何而来需要书的 id”是什么意思。意见表在书 id 正确的页面上?所以你最有可能在你的 POST 的某个地方有这个 id 吗?此外,如果您使用 AJAX,则不需要刷新页面。您可以在意见表上进行 POST,然后关闭 Modal 而无需刷新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-14
    • 1970-01-01
    相关资源
    最近更新 更多