如何使用jquery存储数组中的文本框值? [英] how to store textbox values in array using jquery?
问题描述
我有两个同名的文本框 control_text
,但它们的值不同。我想使用jQuery将所有文本框的值存储在一个数组中。
HTML
< input type =textname =control_textplaceholder =Text Label Imageclass =requiredid =control_textvalue =firstvalue/>
< input type =textname =control_textplaceholder =Text Label Imageclass =requiredid =control_textvalue =secondvalue/>
JavaScript
var test_arr = $(input [name ='control_text']);
$ .each(test_arr,function(i,item){
// i = index,item = array
alert($(item).val());
});
以上代码分别显示文本框的值。我不想单独提醒这些值,我希望立即用逗号分隔符(类似于
(firstvalue,secondvalue)
)提醒。任何帮助表示赞赏。
使用 $ b 附注:单个页面中的元素应具有唯一的 ID,因此请检查您的标记是否有效。 I have two text boxes with the same name HTML JavaScript The above code is displaying the values of text boxes individually. I don't want to alert these values individually, I want to alert both at once with comma separator similar to
Use A side note: elements in a single page should have unique IDs, so check your markup for validity. 这篇关于如何使用jquery存储数组中的文本框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! map() (input [name ='control_text'])。map(function(){
return this.value;
})。get();
control_text
but their values are different. I want to store all my text box values in an array using jQuery.<input type="text" name="control_text" placeholder="Text Label Image" class="required" id="control_text" value="firstvalue" />
<input type="text" name="control_text" placeholder="Text Label Image" class="required" id="control_text" value="secondvalue" />
var test_arr = $("input[name='control_text']");
$.each(test_arr, function(i, item) {
// i = index, item = element in array
alert($(item).val());
});
(firstvalue, secondvalue)
. Any help is appreciated.map()
method:var arr = $("input[name='control_text']").map(function() {
return this.value;
}).get();