【问题标题】:Django Bootstrap Modal Ajax - 1st time it works perfectly, 2nd time - no luckDjango Bootstrap Modal Ajax - 第一次完美运行,第二次 - 不走运
【发布时间】:2020-06-06 02:16:57
【问题描述】:

我正在使用带有 Bootstrap 的 Django 2.2。

这是用于

approach
  1. 打开一个模态窗口,
  2. 用户更新数据,
  3. 用户保存
  4. 更新打开窗口的基础表并关闭模式窗口

到目前为止,一切正常,但是,当您单击相同的按钮打开窗口时会出现问题。在这里,当您单击按钮时,什么也没有发生!

这是 javascript 文件。

            $("#modal-risk-profile").on("submit", ".form-risk-profile", function () {
            var form = $(this);         

            $.ajax({
                url:form.attr("action"),
                data:form.serialize(),
                type:form.attr("method"),
                dataType:'json',
                success: function(data) {
                    console.log(data.form_is_valid);
                    if(data.form_is_valid) {
                        console.log(data.html_planner_client_list);
                        $("#id_client_table tbody").html(data.html_planner_client_list); // if you remove this line, modal window appears without any issues
                        $("#modal-risk-profile").modal("hide");
                        console.log("I have returned with success");
                    }
                    else {
                        console.log("I am in else");
                        $("#modal-risk-profile .modal-content").html(html_form);
                    }                       
                }
            });

            return false;
        }); 

html_planner_client_list的输出如下:

<tr>
<td> Rajesh </td>
<td> 01-04-2000 </td>
<td> Bangalore </td>
<td> ramachandran.test@gmail.com </td>
<td> Salaried </td>
<td class="text-center"> 

        <button class="button-risk-profile btn btn-danger btn-sm py-0" style="font-size:0.9em" data-url="/client/riskprofileplanner/19/">
            Take Questionnaire 
        </button> 


</td>
<td> Subramanian</td>
<td class="text-left"> 
    <a href="/client/client_select/19/" class="btn btn-primary btn-sm py-0" style="font-size: 0.9em;">Select </a>               
    <a href="/client/client_edit/19/" class="btn btn-primary btn-sm py-0" style="font-size: 0.9em;">Edit </a> 
    <a href="/client/client_report/19/" class="btn btn-primary btn-sm py-0" style="font-size: 0.9em;">Report </a>
    <button class="button-choose-planner btn btn-primary btn-sm py-0" style="font-size: 0.9em;" data-url="/client/select_planner/19/">
        Planner 
    </button> 
</td>

Inspector Log 中没有错误,Django 中也没有错误。

可能会出什么问题?

【问题讨论】:

    标签: javascript django ajax modal-dialog


    【解决方案1】:

    我使用link 解决了这个问题:

                $('.button-risk-profile').click(function(){
    
                var btn = $(this);
                console.log("I am in client Risk Profile")
    
                $.ajax({
                  url: btn.attr("data-url"),
                  type: 'get',
                  dataType: 'json',
                  beforeSend: function () {
                    $("#modal-risk-profile").modal("show");
                  },
                  success: function (data) {
                    $("#modal-risk-profile .modal-content").html(data.html_form);
                  }
                });             
    
            });
    

    我改成了:

                $('.client-table').on('click',".button-risk-profile",function(){
    
                var btn = $(this);
                console.log("I am in client Risk Profile")
    
                $.ajax({
                  url: btn.attr("data-url"),
                  type: 'get',
                  dataType: 'json',
                  beforeSend: function () {
                    $("#modal-risk-profile").modal("show");
                  },
                  success: function (data) {
                    $("#modal-risk-profile .modal-content").html(data.html_form);
                  }
                });             
    
            });
    

    【讨论】:

      猜你喜欢
      • 2020-03-28
      • 1970-01-01
      • 1970-01-01
      • 2017-06-16
      • 2018-11-28
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      相关资源
      最近更新 更多