Jquery,循环数组 [英] Jquery, looping over arrays
本文介绍了Jquery,循环数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$('。guestLists')。change ();
var myCounter = parseInt(1);
var arrValues0 = [,, ,;
var arrValues1 = [1,1,1,1];
var arrValues2 = [2,2 $
$。 ){
$('#guestListName'+ myCounter).attr('value',objValue);
myCounter ++;
}
);
});
任何帮助都会很棒。
解决方案
如果要在运行时构造变量名,请使用 eval
:
<$ (
eval(arrValues+ thisId),
...
只需确保其参数的安全性,特别是如果它以某种方式取决于外部/用户输入。在上述情况下,如果 thisId
是一个整数,一切都应该很好。
I'm trying to loop over an array. The array will be different depending on which option you have selected from a selectbox. I can't work out how to select which array to loop over. The bit that's not working is 'arrValues+thisId' inside the each loop.
$('.guestLists').change( function() {
var thisId = $(this).val();
var myCounter = parseInt(1);
var arrValues0 = [ "", "", "", "" ];
var arrValues1 = [ "1", "1", "1", "1" ];
var arrValues2 = [ "2", "2", "2", "2" ];
// Loop over each value in the array.
$.each(
arrValues+thisId,
function( intIndex, objValue ){
$('#guestListName'+myCounter).attr('value',objValue);
myCounter++;
}
);
});
Any help would be great.
解决方案
If you want to construct variable name at runtime, use eval
:
$.each(
eval("arrValues"+thisId),
...
Just ensure the safety of its parameter, especially if it's somehow dependent on external/user input. In the above case, if thisId
is an integer, everything should be fine.
这篇关于Jquery,循环数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文