繁体   English   中英

Bootstrap动态表不起作用

[英]Bootstrap dynamic table not working

我想在代码中使用Bootstrap动态表。 这是HTML的代码:

<button type="button" id="show">Change Content</button>
<div class="table-responsive">
   <table id="tabl" class="table table-condensed"></table>            
</div>

和JS代码:

$(document).ready(function() {

$('.deliverable-infos').hide();
$('.dropdown-deliverable').on('click', function(e) {
    console.log("dropdown toggled!");
    e.preventDefault();
    e.stopPropagation();
    //get targeted element via data-for attribute
    var dataFor = $(this).data('for');
    var idFor = $(dataFor);
    idFor.slideToggle();
}); 

});
 $('#show').click(function(){
  var table="<thead><tr><th>Deliverable</th><th>Description</th><th>Ending on</th></tr></thead>";
                table+="<tr class='clickable warning dropdown-deliverable' data-for='#details_1'><td>uranium</td><td>magic potion</td><td>April 23, 2014</td></tr><tr style='padding:0'><td style='padding:0px'></td><td colspan=2 style='padding:0px;'><div class='deliverable-infos' id='details_1'><table class='table table-condensed table-user-content' id='hidden_table_1'><tr><td>نام کالا :</td><td class='right-col'>April 22, 2013</td></tr><tr><td>قیمت :</td><td class='right-col'>500 h</td></tr><tr><td>تخفیف :</td><td class='right-col'>3000 €</td></tr></table></div></td><td style='padding:0'></td><td style='padding:0'></td></tr><tr class='clickable warning dropdown-deliverable' data-for='#details_2'><td>detonator</td><td>magic wand</td><td>April 23, 2014</td></tr><tr style='padding:0'><td style='padding:0'></td><td colspan=2 style='padding:0'><div class='deliverable-infos' id='details_2'><table class='table table-condensed table-user-content' id='hidden_table_2'><tbody><tr><td>Started:</td><td class='right-col'>April 22, 2013</td></tr><tr><td>Load:</td><td class='right-col'>20 h</td></tr><tr><td>Fare:</td><td class='right-col'>200 €</td></tr></tbody></table></div></td><td style='padding:100px'></td><td style='padding:100px'></td></tr>";
                document.getElementById("tabl").innerHTML = table;
 });

但是,当我运行它时,它向我显示了所有表和动态端都无法正常工作。

当您在网页上使用动态内容时,您需要使用以下方法来添加事件侦听器:

$(document).on('click', '.dropdown-deliverable', function(e) {
});

代替:

$('.dropdown-deliverable').on('click', function(e) {

});

然后它将起作用。

在此处阅读有关动态事件侦听器的更多信息: http : //api.jquery.com/on/#direct-and-delegated-events

更新使用时:

$(document).on('click', '.dropdown-deliverable', function(e) {
});

$(document)可以用页面中没有动态生成的任何静态元素替换。

例如,您可以使用表id =“ tabl”,因为它在页面上不是动态的,因此您的代码变为:

$('#tabl').on('click', '.dropdown-deliverable', function(e) {
});

暂无
暂无

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

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