【发布时间】:2014-01-04 01:42:11
【问题描述】:
我已经解决这个问题 4 个小时了,并决定在这里寻求帮助。非常感谢您的帮助
有一个页面,我在其中设置了一个包含信息和按钮的表格。按钮从引导程序启动一个模式,允许用户编辑表格信息。当用户完成编辑时,模式关闭,并且使用 ajax 更新表格。一切都按预期工作,直到表格被 ajax 更新。更新表上的按钮也会在 ajax 调用中被替换,我的理论是当点击被替换的按钮时,javascript 无法检测到新按钮。
所以主要问题是:当使用按钮更新表格时,我怎样才能让引导程序检测到新按钮,所以按钮将作为模态响应而不是直接指向链接?
模态关闭时:
$('#testModal').on('hidden.bs.modal', function () {
calc();
users();
})
对表的 ajax 调用:
function users(){
$.ajax({
type : "POST",
url : "/users.php",
data : regning })
.done(function( data ) {
$('#users').html(data);
});
}
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>
【问题讨论】:
-
当您第一次运行 jquery 以绑定到新的 dom 元素时,它们可能不存在。所以尝试再次运行 jquery.on() 看看是否有帮助
-
jquery.on() 应该在代码的什么位置?
标签: javascript jquery ajax twitter-bootstrap