jQuery:如何将变量分配回文档中的隐藏元素?
如何将变量(单击的按钮 id)分配给 jQuery Diaglog,然后返回到文档中的隐藏元素(myhiddenid)以供以后使用?
<table border="0" class="time_data">
<td><button type="button" id='001' class="del-fruit" > Apple </td>
<td><button type="button" id='002' class="del-fruit" > Banana </td>
<td><button type="button" id='003' class="del-fruit" > Cantalope </td>
</table>
<div id='myhiddenid' style="display:none;"></div>
<script type="text/javascript">
$("#dialog-form").dialog({
autoOpen: false,
height: 150,
width: 350,
modal: true,
resizable: false,
buttons: {
'Yes': function() {
var bValid = true;
allFields.removeClass('ui-state-error');
bValid = bValid
if (bValid) {
//the assining of myhiddenid should be here!!
//and should contain the ID of the button that i clicked.
$(this).dialog('close');
}
},
No: function() {
$(this).dialog('close');
}
},
close: function() {
allFields.val('').removeClass('ui-state-error');
}
});
$('.del-fruit')
.button()
.click(function() {
$('#dialog-form')
.dialog( "option", "title", this.id )
.dialog('open');
});
</script>
How can I assign a variable (button id that was clicked), to the jQuery Diaglog, then back to the hidden element (myhiddenid) in the document for later use?
<table border="0" class="time_data">
<td><button type="button" id='001' class="del-fruit" > Apple </td>
<td><button type="button" id='002' class="del-fruit" > Banana </td>
<td><button type="button" id='003' class="del-fruit" > Cantalope </td>
</table>
<div id='myhiddenid' style="display:none;"></div>
<script type="text/javascript">
$("#dialog-form").dialog({
autoOpen: false,
height: 150,
width: 350,
modal: true,
resizable: false,
buttons: {
'Yes': function() {
var bValid = true;
allFields.removeClass('ui-state-error');
bValid = bValid
if (bValid) {
//the assining of myhiddenid should be here!!
//and should contain the ID of the button that i clicked.
$(this).dialog('close');
}
},
No: function() {
$(this).dialog('close');
}
},
close: function() {
allFields.val('').removeClass('ui-state-error');
}
});
$('.del-fruit')
.button()
.click(function() {
$('#dialog-form')
.dialog( "option", "title", this.id )
.dialog('open');
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用隐藏字段并为其分配值,而不是 div:例如
HTML
jQuery
Instead of a div, you can use hidden field and assign it's value: For e.g.
HTML
jQuery
同意第一个答案,但如果你想使用隐藏的 div:
set:
get:
Agreed with first answer, but if you want to use a hidden div:
set:
get:
另一种方法是使用 jQuery .data() 函数来存储针对元素的信息,例如针对 #dialog-form 本身,并使用它来表示从对话框返回的信息。如果您发现自己需要返回的不仅仅是一个简单的 ID,这会很方便,因为您可以直接存储对象,而不必从隐藏字段来回序列化信息。
以下是基于 HTML 的示例: http://jsbin.com/omexi5
当文档加载时,每个按钮表中的 被分配了一个单击处理程序,该处理程序首先将其 ID 记录在对话框的 .data() 中,然后打开对话框。对话框的“打开”事件处理程序使用 .data() 中的 ID 在对话框内设置消息,然后“是”处理程序使用它来查找和更新表按钮的文本。
希望有帮助。
An alternative approach would be to use the jQuery .data() function to store information against an element, for instance against the #dialog-form itself, and use it to represent the information coming back from the dialog. This would be handy if you found yourself needing to return more than just a simple ID, because you could store an object directly rather than having to serialise the information back and forth from hidden fields.
Here is an example based on your HTML: http://jsbin.com/omexi5
When the document loads, each button in the table is assigned a click handler that first records it's ID in the dialog's .data() and then it opens the dialog. The dialog's 'open' event handler uses the ID from .data() to set the message inside the dialog and then the Yes handler uses it to find and update the table button's text.
Hope it helps.