[英]Materialize.css Modal is Not Initializing Correctly on Dynamically Created Elements
I'm using an ajax call to append rows dynamically to a table using jQuery. 我正在使用ajax调用来使用jQuery将行动态追加到表中。 I would like the button generated in each row to display a modal with data specific to the items in that particular row. 我希望在每一行中生成的按钮显示一个模式,其中包含特定于该特定行中项目的数据。
I am currently running into the following issue: 我目前遇到以下问题:
-The modal appears when I click on the 1st & 2nd buttons. -当我单击第一和第二按钮时,模态出现。 However, when I try to close out of the 2nd modal, the box shadow from the modal does not go away, and I can no longer continue clicking on any buttons until I refresh the page. 但是,当我尝试关闭第二个模态时,模态中的框阴影不会消失,在刷新页面之前,我无法继续单击任何按钮。
I checked the Materialize.css documentation and it mentions that we need to initialize the modal with $(".modal-trigger").leanModal() if using a trigger to show a modal. 我查看了Materialize.css文档,其中提到如果使用触发器显示模态,则需要使用$(“。modal-trigger”)。leanModal()初始化模态。 The ID that I am using to trigger the modal is called "nutrition-facts" in the code below. 我用来触发模式的ID在下面的代码中称为“营养事实”。
Any help would be appreciated. 任何帮助,将不胜感激。 Thanks!! 谢谢!!
UPDATE: With CodePen: http://codepen.io/mtaggart89/pen/pgbgOB 更新:使用CodePen: http ://codepen.io/mtaggart89/pen/pgbgOB
HTML: HTML:
<!-- Modal Structure -->
<div id="nutrition-facts" class="modal modal-fixed-footer">
<div class="modal-content">
<h4>Nutrition Facts</h4>
<p>A bunch of text</p>
</div>
<div class="modal-footer">
<a href="#!" class="modal-action modal-close waves-effect waves-green btn-flat">Close</a>
</div>
</div>
jQuery Function to Create Elements: jQuery函数创建元素:
function buildTable(foodData) {
var itemList = foodData.list.item;
var foodGroup, foodName, newDiv, createButton, ndbNumber, createTable, tableHead, categoryHeading, nameHeading, tr;
$("table").addClass("bordered");
categoryHeading = $("<th>").html("Category");
nameHeading = $("<th>").html("Name");
$("thead").append(categoryHeading).append(nameHeading);
for (var i = 0; i < itemList.length; i++) {
foodGroup = $("<td>").html(itemList[i].group);
foodName = $("<td>").html(itemList[i].name);
ndbNumber = itemList[i].ndbno;
newDiv = $("<td>");
createButton = $("<a>")
.addClass("waves-effect waves-light btn cyan nutrition modal-trigger")
.attr("href", "#nutrition-facts")
.html("Nutrition Facts")
.attr('data-ndbnum', ndbNumber);
addButton = newDiv.append(createButton);
tr = $("<tr>").append(foodGroup).append(foodName).append(addButton);
$("tbody").append(tr);
}
}
jQuery Event Listener: jQuery事件监听器:
$(document).on('click', '.nutrition', function(e){
e.preventDefault();
var ndbNumber = $(this).attr('data-ndbnum');
$(".modal-trigger").leanModal();
});
http://materializecss.com/modals.html http://materializecss.com/modals.html
Please change code with 请使用
$(document).on('click', '.nutrition', function(e) {
e.preventDefault();
var ndbNumber = $(this).attr('data-ndbnum');
//you have to trigger modal like this
//$(".modal-trigger").leanModal();
$('#nutrition-facts').openModal();
});
http://codepen.io/anon/pen/YwWwdx http://codepen.io/anon/pen/YwWwdx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.