我想遍历具有动态名称值的元素列表,比如item1,item2等,但我像下面那样得到了undefined。通过循环名称选择器获取输入值
len = $('.aws').length + 1;
var obj = [],
temp = {};
for (var i = 1; i <= len; i++) {
console.log(i)
temp["index"] = $('.aws[name="item' + i + '"]').val()
obj.push(temp);
}
console.log(obj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" class="aws" name="item1" value="1.jpg">
<input type="hidden" class="aws" name="item2" value="2.jpg">
为什么不使用'。每()'? – Twisty
另外,你的第二个'class'不匹配。想想你们都想成为'aws'。 – Twisty
[将新对象添加到数组]可能重复(http://stackoverflow.com/questions/9543805/add-new-object-to-array) –