简体   繁体   English

Bootstrap Modal未检测到称为按钮的ajax

[英]Bootstrap Modal not detecting ajax called buttons

I've been fighting this issue for 4 hours now, and decided to seek help here. 我已经为这个问题解决了4个小时,因此决定在这里寻求帮助。 Help is much appreciated 非常感谢帮助

Got a page where i've set up a table with information and buttons. 在页面上我建立了带有信息和按钮的表格。 The buttons starts up a modal from bootstrap, which allows the user to edit the tables information. 这些按钮从引导程序启动模式,使用户可以编辑表信息。 When the user is done editing, the modal closes, and the table is updated, using ajax. 当用户完成编辑后,模态将关闭,并使用ajax更新表。 All works as intended, right until the table is updated by ajax. 所有操作均按预期进行,直到由ajax更新表为止。 The buttons on the updated table are replaced on the ajax call as well, and my theory is that when clicking on the replaced buttons, javascript cannot detect the new buttons. 更新表上的按钮也将在ajax调用中替换,我的理论是,当单击替换的按钮时,javascript无法检测到新按钮。

So the main issue is: When updating a table with buttons, how can i make bootstrap detect the new buttons, so the buttons will respond as modals and not head directly to the link? 因此,主要问题是:使用按钮更新表时,如何使引导程序检测新按钮,以便按钮将作为模态响应而不直接进入链接?

When modal is closing: 当模式关闭时:

$('#testModal').on('hidden.bs.modal', function () {
            calc();
            users();

        })

ajax call on table: 表上的ajax调用:

function users(){
      $.ajax({ 
        type  :   "POST", 
        url   :   "/users.php", 
        data  :   regning })
      .done(function( data ) {
        $('#users').html(data);
      });
    }

inside users.php: 在users.php内部:

<div class="span4 offset2">
                    <h2>Regnskab</h2>
                    <div style="background-color: white; border: 1px solid lightgrey; border-radius: 5px; margin-bottom: 10px;">
                            <table>
                                <?php
                                $totalamount = 0;
                                $participants = 0;
                                while($row = mysqli_fetch_object($users)):?>
                                <?php $spendings = $db->get_spendings($regning, $row->user_id);?>
                                <tr <?php if(mysqli_num_rows($spendings) == 0){ echo "style='border-bottom: 1px solid lightgrey;'";} ?>>
                                  <td class='span2 spendingname_responsive'>
                                      <a href="/edit_user.php?user_id=<?php echo $row->user_id."&p=".$bill_code."&bill_id=".$regning ?>" id="ajax" role="button" style="text-decoration: none;"><h4 style="margin-top: 0px; margin-bottom: 0px;"><?php echo $row->user_name; ?></h4></a>
                                  </td>
                                  <?php $totalamount1 = $db->get_totalamount_user($row->user_id);?>
                                  <td class='span2 spendingamount_responsive' style='text-align: right; color: #1ABC9C;'>
                                      <?php echo $totalamount1->spendings." kr."; ?>
                                  </td>
                                  <td class='span1' style='text-align: right;'>
                                    <a href="/new_spending.php?bill_id=<?php echo $regning."&user_id=".$row->user_id."&p=".$bill_code ?>" id="ajax" role="button" class="btn btn-primary"><i class="fui-plus-16"></i></a>
                                  </td>
                                </tr>
                                 <?php 
                                 $rowspendnum = 1;
                                 $rowsspendings = mysqli_num_rows($spendings);
                                 while($innerrow = mysqli_fetch_object($spendings)):?>
                                <tr 
                                <?php
                                if($rowsspendings == 1){
                                    echo "style='border-bottom: 1px solid lightgrey;';";
                                }
                                else if($rowsspendings == $rowspendnum){
                                    echo "style='border-bottom: 1px solid lightgrey;';";
                                }
                                ?>
                                >
                                    <td class='span2 spendingname_responsive' style="padding-top: 0px;">
                                        <?php echo $innerrow->spending_name; ?>
                                    </td>
                                    <td class='span2 spendingamount_responsive' style='text-align: right; padding-top: 0px;'>
                                        <?php echo $innerrow->spending_amount." kr."; ?>
                                    </td>  
                                    <td class='span1' style='text-align: right; padding-top: 0px;'>
                                        <a href="/edit_spending.php?spending_id=<?php echo $innerrow->spending_id."&p=".$bill_code ?>" id="ajax" role="button" class="btn btn-default"><i class="fui-settings-16"></i></a>
                                    </td>
                                </tr>
                                <?php 
                                $rowspendnum++;
                                endwhile ?>
                                <?php $totalamount = $totalamount + $totalamount1->spendings; ?>
                                <?php $participants++; ?>  
                                <?php endwhile ?>
                            </table>
          <?php $average = $totalamount/$participants; ?>
        </div>
          <a href="/new_user.php?bill_id=<?php echo $regning."&p=".$bill_code ?>" id="ajax" role="button" class="btn btn-primary btn-block" style="border-radius: 5px; padding: 6px 12px;">
              Tilføj person
          </a>
</div>

I suggest you read the doucmentation for .on() very closely: 我建议您仔细阅读.on()的文档:

http://api.jquery.com/on/ http://api.jquery.com/on/

Specifically this part: 特别是这部分:

Event handlers are bound only to the currently selected elements; 事件处理程序仅绑定到当前选定的元素; they must exist on the page at the time your code makes the call to .on(). 当您的代码调用.on()时,它们必须存在于页面上。 To ensure the elements are present and can be selected, perform event binding inside a document ready handler for elements that are in the HTML markup on the page. 为了确保元素存在并可以选择,请在文档就绪处理程序内对页面上HTML标记中的元素执行事件绑定。 If new HTML is being injected into the page, select the elements and attach event handlers after the new HTML is placed into the page. 如果将新HTML注入到页面中,请在将新HTML放入页面后选择元素并附加事件处理程序。 Or, use delegated events to attach an event handler, as described next. 或者,使用委托事件来附加事件处理程序,如下所述。

You would need to delegate that behavior to the table itself, so that any children buttons would behave the same way. 您需要将该行为委托给表本身,以便所有子按钮的行为都相同。 A very generic and non-selective example: http://jsfiddle.net/Kxzt6/ 一个非常普通且非选择性的示例: http : //jsfiddle.net/Kxzt6/

$('table').on('click', '#table-button', function(e){
  e.preventDefault()
  console.log("hello!")
})


<table>
  <td><button id="table-button">Hi!</button></td>
<table>

In the jsfiddle I include a button to insert a new button onto the dom. 在jsfiddle中,我包括一个将新按钮插入dom的按钮。 Without delegation that new button would not console log our wonderful message, but since we delegated it to the table any children #table-button's would know how to say hello. 没有委派,新按钮将无法控制台记录我们的精彩信息,但是由于我们将其委派给了表,因此任何#table-button子级都会知道如何打招呼。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM