如何使用jquery存储数组中的文本框值? [英] how to store textbox values in array using jquery?

查看:173
本文介绍了如何使用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 map() (input [name ='control_text'])。map(function(){
return this.value;
})。get();

附注:单个页面中的元素应具有唯一的 ID,因此请检查您的标记是否有效。


I have two text boxes with the same name control_text but their values are different. I want to store all my text box values in an array using jQuery.

HTML

<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" />

JavaScript

var test_arr = $("input[name='control_text']");
$.each(test_arr, function(i, item) {
    // i = index, item = element in array
    alert($(item).val());
});

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 (firstvalue, secondvalue). Any help is appreciated.

解决方案

Use map() method:

var arr = $("input[name='control_text']").map(function() {
    return this.value;
}).get();

A side note: elements in a single page should have unique IDs, so check your markup for validity.

这篇关于如何使用jquery存储数组中的文本框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆