我有多个输入字段,如下所示:
<input type="text" name="card[]">
<input type="text" name="card[]">
<input type="text" name="card[]">
用户可以根据需要添加或删除这些字段,因此字段的名称是一个数组。 要获得数组的长度,这可以正常工作:
var n = $("input[name^= 'card']").length;
如何从数组中读取值?
我尝试了这个不起作用:
var n = $("input[name^='card']").length;
var array = $("input[name^='card']");
for(i=0;i<n;i++)
{
card_value= array[i].val();
alert(card_value);
}
这也不起作用:
var n = $("input[name^='card']").length;
for(i=0;i<n;i++)
{
card_value= $("input[name^='card["+i+"]']").val();
alert(card_value);
}
如何从此数组中读取值? 救命啊!
答案 0 :(得分:13)
使用地图功能
//routes/addusers.js (in the actions hook after getting properties from the controller)
var user = store.createRecord('user', {
id: userData.uid,
name: name
});
var responseSet = store.createRecord('response-set', {
survey: survey,
respondentid: user
});
user.setProperties({
'respSetSurveyLookup': [{'surveyId': _this.currentModel.get('id'),
'respSetId': responseSet.get('id')}],
'surveysToTake': [_this.currentModel]
});
_this.currentModel.get('respondents').pushObject(user);
Ember.RSVP.all([user.save(), _this.currentModel.save(), responseSet.save()])
.then(function(){
controller.setProperties({ multipleUsers: '', showAddUsers: false});
});
答案 1 :(得分:12)
您应该使用:
card_value= array.eq(i).val(); //gets jquery object at index i
或
card_value= array[i].value; //gets dom element at index i
答案 2 :(得分:7)
jQuery
个集合有一个带有.each
的内置迭代器:
$("input[name^='card']").each(function () {
console.log($(this).val());
}
答案 3 :(得分:0)
var n = $("input[name^='card']").length;
var array = $("input[name^='card']");
for(i=0; i < n; i++) {
// use .eq() within a jQuery object to navigate it by Index
card_value = array.eq(i).attr('name'); // I'm assuming you wanted -name-
// otherwise it'd be .eq(i).val(); (if you wanted the text value)
alert(card_value);
}
答案 4 :(得分:0)
您可以循环浏览项目:
答案 5 :(得分:0)
您的语法不正确。
card_value = $(array[i]).val();
或card_value = array[i].value;
array [i]不是jQuery对象(出于某种原因)。
检查浏览器的控制台可能会对此有所帮助。
答案 6 :(得分:0)