嗨,我是Jquery的新手。我想添加两个变量。两者都是全球变数。 请参阅此代码末尾的“添加变量”。 “cashupfirst”变量显示核心值,但“cashupsecond”变量显示“undefined”。但是我可以在此代码之前显示“cashupsecond”的正确值。请帮忙。
jQuery(function($) {
var cashupfirst, cashupsecond;
var a, parent, input, doneLink, b, i, eq, c, z;
$(".Cashups").delegate("td:eq(3) > a", "click", function(event) {
//Index reading(not used here)
//var eq = $(this).parent().children("a").index(this);
// The `a` has been clicked; cancel the action as
// we're handling it
event.preventDefault();
event.stopPropagation();
// Remember it and its parent
a = $(this);
parent = a.parent();
// Insert an input in front of it, along with a done link
// because blur can be problematic
input = $("<input type='text' size='10'>");
input.insertBefore(a);
input.blur(doneHandler);
doneLink = $("<a href='#'>done</a>");
doneLink.insertBefore(a);
doneLink.click(doneHandler);
// Put the text of the link in the input
input.val(a.text());
// Temporarily detach the link from the DOM to get it
// out of the way
a.detach();
// Give the input focus, then wait for it to blur
input[0].focus();
// Our "done" handler
function doneHandler() {
// Replace the content of the original link with
// the updated content
a.text(input.val());
cashupfirst = a.text();
alert(cashupfirst);
//Set cookie to pass the text value to update it to table permanently
$.cookie('demoCookie',cashupfirst,{expires: 7, path: '/'});
// Put the link back, remove our input and other link
a.insertBefore(input);
input.remove();
doneLink.remove();
}
});
$(".Cashups").delegate("td:eq(9) > a", "click", function(event) {
// The `a` has been clicked; cancel the action as
// we're handling it
event.preventDefault();
event.stopPropagation();
// Remember it and its parent
a = $(this);
parent = a.parent();
// Insert an input in front of it, along with a done link
// because blur can be problematic
input = $("<input type='text' size='10'>");
input.insertBefore(a);
input.blur(doneHandler);
doneLink = $("<a href='#'>done</a>");
doneLink.insertBefore(a);
doneLink.click(doneHandler);
// Put the text of the link in the input
input.val(a.text());
// Temporarily detach the link from the DOM to get it
// out of the way
a.detach();
// Give the input focus, then wait for it to blur
input[0].focus();
// Our "done" handler
function doneHandler() {
// Replace the content of the original link with
// the updated content
a.text(input.val());
cashupsecond = a.text();
alert(cashupsecond);
//Set cookie to pass the text value to update it to table permanently
$.cookie('demoCookie1',cashupsecond,{expires: 7, path: '/'});
// Put the link back, remove our input and other link
a.insertBefore(input);
input.remove();
doneLink.remove();
}
//Add the variables
if (cashupfirst!= '' && cashupsecond!= '') {
alert(cashupfirst);
alert(cashupsecond);
var xyz = (parseInt(cashupfirst) + parseInt(cashupfirst));
jQuery('td#cashcalculator_total a').html(xyz);
}
});
//alert(cashupsecond);
});
答案 0 :(得分:1)
尝试
var xyz = parseInt(cashupfirst) + parseInt(cashupsecond);
alert
函数不会返回已警告的内容。
编辑:
好的,我明白了什么是错的。您必须将变量添加到第二个doneHandler函数中,否则它将不会等到您收到cashupsecond的值:
// Our "done" handler
function doneHandler() {
...
//Add the variables
if (cashupfirst!= '' && cashupsecond!= '') {
alert(cashupfirst);
alert(cashupsecond);
var xyz = parseInt(cashupfirst) + parseInt(cashupsecond);
jQuery('td#cashcalculator_total a').html(xyz);
}
}
// Don't add the variables here.