[英]jquery: passing parameter to modal
I have the following in a table, repeated for each row: 我在表中有以下内容,每行重复一次:
<a <?php echo 'id="'.$id.'"'; ?> class="custom-dialog-btn btn btn-small">
<i class="icon-trash"></i>
</a>
where id
is different for each line, because it is taken from a table in database where it is the primary key. 其中id
对于每一行是不同的,因为它是从数据库中的表中获取的,它是主键。
Then, I used th following jQuery code: 然后,我使用了以下jQuery代码:
$(".custom-dialog-btn").bind("click", function (event) {
$("#mws-jui-dialog").dialog("option", {
modal: false
}).dialog("open");
event.preventDefault();
});
$("#mws-jui-dialog").dialog({
autoOpen: false,
title: "Alert",
modal: true,
width: "640",
buttons: [{
text: "NO",
id: "cancel_button",
click: function () {
$(this).dialog("close");
}
},
{
text: "OK",
id: "confirm_button",
click: function () {
myremovefuntion(id); // I need the id
}}
]
});
that refers to the dialog: 指的是对话框:
<div id="mws-jui-dialog">
<div class="mws-dialog-inner">
<h2>Are you sure you want to delete?</h2>
</div>
</div>
How can I pass the id to the modal? 我怎样才能将id传递给模态?
You can append a data attribute to your dialog div as follows; 您可以将数据属性附加到对话框div,如下所示;
$('a').click(function (e) {
e.preventDefault();
$("#your_dialog").data('mycustomdata', $(this).prop('id')).dialog('open');
});
And retreive it like this 然后像这样回顾它
$("#your_dialog").dialog({
autoOpen: false,
resizable: false,
height:200,
modal: true,
buttons: {
Cancel: function() {
$(this).dialog('close');
},
'Delete': function() {
$(this).dialog('close');
var mydata = $(this).data('mycustomdata'); // = gives you the id of anchor element
// some delete logic
}
}
});
Use the following code to get the id: 使用以下代码获取id:
var id = $(this).prop("id");
And you will get the id of that which element you have clicked. 并且您将获得您点击的元素的ID。
While Emin's answer certainly works, I wonder if this fabuolus jQuery UI library perhaps has a dialogue flavour analogue to JavaScript's native confirm()
method? 虽然Emin的答案肯定有效,但我想知道这个fabuolus jQuery UI库是否具有类似于JavaScript的本机confirm()
方法的对话风格? Or perhaps a possibility to pass in callback functions? 或者可能传递回调函数? This would remove the need from the dialogue code containing business logic. 这将消除包含业务逻辑的对话代码的需要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.