是否可以将事件添加到jqueryui对话框小部件内动态创建的按钮中?
tmp7 += " <div class='box' style='border:1px solid ;padding: 5px; margin: 5px'>";
tmp7 += "<input type='button' name='btnedit' class='editbtn' value='Edit' id='btnedit'> ";
tmp7 += " <input type='button' name='btndelete' value='Delete' id='btndelete''> ";
tmp7 += "</div>";
var newDiv = $(document.createElement('div'));
$(newDiv).html(tmp7);
$(newDiv).dialog({
//add button events here.
});
请考虑以下示例。
$(function() {
$("button").click(function() {
var fs = {
name: "document.txt"
};
var newDiv = $("<div>", {
title: "Action Options"
}).html("Please select an action for " + fs.name).dialog({
buttons: [{
text: "Edit",
click: function() {
//editFile(fs);
$(this).dialog("close");
}
}, {
text: "Delete",
click: function() {
//deleteFile(fs);
$(this).dialog("close");
}
}, {
text: "Cancel",
click: function() {
$(this).dialog("close");
}
}]
});
});
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<button>Action</button>
类似示例:https://jqueryui.com/dialog/#modal-confirmation
您已经创建了将出现在对话框内容中的元素。如果愿意,您仍然可以将click
事件绑定到这些元素。就个人而言,我将使用对话框上的按钮。