如何将javascript变量发送到后续的jquery函数?这是我的密码。
<script type="text/javascript">
$(function() {
var name = $("#name"),
email = $("#email"),
password = $("#password"),
itemid = $("#itemid"),
tips = $(".validateTips");
function updateTips(t) {
tips
.text(t)
.addClass('ui-state-highlight');
setTimeout(function() {
tips.removeClass('ui-state-highlight', 1500);
}, 500);
}
$("#dialog-form").dialog({
autoOpen: false,
height: 320,
width: 350,
modal: true,
/*
buttons: {
'Change category': function() {
alert("The itemid2 is "+itemid2);
var bValid = true;
$('#users tbody').append('<tr>' +
'<td>' + name.val() + '</td>' +
'<td>' + email.val() + '</td>' +
'<td>' + password.val() + '</td>' +
'<td>' + itemid.val() + '</td>' +
'</tr>');
$(this).dialog('close');
},
Cancel: function() {
$(this).dialog('close');
}
},
*/
close: function() {
allFields.val('').removeClass('ui-state-error');
}
});
$('.changecategory')
.button()
.click(function() {
var categoryid = $(this).attr("categoryid");
var itemid = $(this).attr("itemid");
var itemid2 = $(this).attr("itemid");
var itemtitle = $(this).attr("itemtitle");
var parenttag = $(this).parent().get(0).tagName;
var removediv = "itemid_" +itemid;
alert("The itemid is "+itemid);
$('#dialog-form').dialog('open');
});
});
</script>我来把它拆开。
.changecategory部分首先在调用页面上的图像为clicked.
我是否可以将一个变量传递给另一个jquery函数,而不必使用javascript设置cookie,然后使用jquery读取它呢?
发布于 2010-05-10 14:36:12
您的dialog('open')调用将无法看到您定义的任何itemid变量,因为它们处于完全不同的闭包中。
如果你告诉我们你使用的是哪个对话框插件,我们可以帮你解决一些问题。如果是jQuery UI对话框小部件,那么我建议您这样做:
$('.changecategory')
.button()
.click(function() {
var scope = $(this); // for the sake of code speed
var categoryid = scope.attr("categoryid");
var itemid = scope.attr("itemid");
var itemid2 = scope.attr("itemid");
var itemtitle = scope.attr("itemtitle");
var parenttag = scope.parent().get(0).tagName;
var removediv = "itemid_" +itemid;
alert("The itemid is "+itemid);
$('#dialog-form').dialog('option', 'itemId', itemId); // store the id
$('#dialog-form').dialog('open');
});因此,在打开对话框之前,您可以设置一些对话框的内部选项,这些选项可以在open函数的闭包中作为对话框的选项使用。
另一种(不推荐的方法)是使用@Tgr对全局的方法。
发布于 2010-05-10 14:14:19
将整个$("#dialog-form").dialog({嵌套在$('.changecategory')的click事件中
然后,您将能够引用对话框中的itemid。
发布于 2010-05-10 14:24:52
您可以通过使用window对象的属性来模拟全局变量:window.itemid = $('#itemid'); (实际上,函数范围中没有定义的变量是window的属性)。
您还可以在单击处理程序中使用data()函数jQuery:$('#dialog-form').data('itemid', itemid);为DOM对象赋值,然后在对话框中为var itemid = $('#dialog-form').data('itemid');赋值。
https://stackoverflow.com/questions/2803223
复制相似问题