我有以下代码:
$("input.isDebt").click(function() {
var totalAmount_test = $("#CC_TotalAmount_form").val();
var total = 0;
$("input.isDebt:checked").each(function() {
total = parseFloat($(this).val());
});
totalAmount_test = parseFloat(totalAmount_test)+total;
});
$("input.isDebt").blur(function() {
sum = totalAmount_test + parseFloat($("#CC_TotalAmount_form").val());
$("#CC_TotalAmount_form").val(sum.toFixed(2));
});
});
我需要totalAmmount_test
从$("input.isDebt:checked").each(function() { ... })
传递到函数$("input.isDebt").blur(function() { ... })
。
关于我如何做到这一点的任何想法?
答案 0 :(得分:1)
最好的方法是使用.data()
(假设您使用的是jQuery)。关于here
$("input.isDebt").click(function() {
var totalAmount_test = $("#CC_TotalAmount_form").val();
var total = 0;
$("input.isDebt:checked").each(function() {
total = parseFloat($(this).val());
});
totalAmount_test = parseFloat(totalAmount_test)+total;
$(this).data('totalAmount_test', totalAmount_test);
});
$("input.isDebt").blur(function() {
totalAmount_test = $(this).data('totalAmount_test');
sum = totalAmount_test + parseFloat($("#CC_TotalAmount_form").val());
$("#CC_TotalAmount_form").val(sum.toFixed(2));
});
答案 1 :(得分:0)
不确定..
var totalAmount_test = 0;
$("input.isDebt").click(function() {
totalAmount_test = $("#CC_TotalAmount_form").val();
var total = 0;
$("input.isDebt:checked").each(function() {
total = parseFloat($(this).val());
});
totalAmount_test = parseFloat(totalAmount_test)+total;
});
$("input.isDebt").blur(function() {
sum = totalAmount_test + parseFloat($("#CC_TotalAmount_form").val());
$("#CC_TotalAmount_form").val(sum.toFixed(2));
});
});
将是最简单的方法。
答案 2 :(得分:0)
创建一个具有数据属性的元素,确保将自定义属性的前缀加上'data-',使其符合HTML5标准
$("input.isDebt").click(function() {
var totalAmount_test = $("#CC_TotalAmount_form").val();
var total = 0;
$("input.isDebt:checked").each(function() {
total = parseFloat($(this).val());
});
totalAmount_test = parseFloat(totalAmount_test)+total;
// creates a hidden element w/the amount
$("<div id='total-amount' style='display:none'></div>").attr("data-total-amount", totalAmount_test);
});
$("input.isDebt").blur(function() {
// retrieve the value
var totalAmount = $("#total-amount").attr("data-total-amount");
// remove the div if you don't need it anymore
$("#total-amount").remove();
sum = totalAmount_test + parseFloat($("#CC_TotalAmount_form").val());
$("#CC_TotalAmount_form").val(sum.toFixed(2));
});
});