如何在jQuery中为这些阶段编写一个dry update_output()函数?
所以我在另一个问题上得到了帮助,但当我的应用程序变得越来越复杂时,我正在努力实现它如何在jQuery中为这些阶段编写一个dry update_output()函数?,jquery,function,dry,Jquery,Function,Dry,所以我在另一个问题上得到了帮助,但当我的应用程序变得越来越复杂时,我正在努力实现它 var gender; var age; $("#gender :radio").click(function() { gender = $(this).val(); update(); }); $("#age li").click(function() { age = $(this).text(); $('#children').show(); update(); }); functio
var gender;
var age;
$("#gender :radio").click(function() {
gender = $(this).val();
update();
});
$("#age li").click(function() {
age = $(this).text();
$('#children').show();
update();
});
function update() {
var text = gender || "";
if (text.length > 0) {
text += " -> ";
}
text += age || "";
$("span.jquery_out").text(text);
}
这很好,但我想添加12个变量,我需要以更理想的方式让update()函数计时
我的VAR是:
var gender;
var age;
var children;
var income;
var stage2select;
var day;
var spend;
var categories;
var product;
var price;
var quantity;
var total;
如何让这些函数在update()函数中循环使用“->”,而不过度使用代码
谢谢,Matt。您可以将长变量列表更改为对象,然后像这样循环对象:
var myObj = {
gender:'',
age:'',
children:'',
income:'',
stage2select:'',
day:'',
spend:'',
categories:'',
product:'',
price:'',
quantity:'',
total:''
};
$("#gender :radio").click(function() {
myObj.gender = $(this).val();
update();
});
$("#age li").click(function() {
myObj.age = $(this).text();
$('#children').show();
update();
});
function update() {
var text = "";
$.each(myObj, function(i){
if (this != ''){
if (text.length){
text += " -> ";
}
text += this;
}
});
$("span.jquery_out").text(text);
}
请,请修复您的shift键,我下次不会为您编辑它。您好,谢谢,但有什么问题,只是为了确保我不再这样做??您又这样做了xD您在您的评论中用小写字母写了我。问题是读起来真的很痛苦。谢谢,只是“var out”应该是“var text”,我猜是吧?