开发者

Get value of inputs with Jquery

开发者 https://www.devze.com 2022-12-21 23:02 出处:网络
I have 100 inputs with the name table[]. How can I get their value with jQuery, as an array? I am trying to do something like $_POST[\'table\'] in PHP.

I have 100 inputs with the name table[]. How can I get their value with jQuery, as an array?

I am trying to do something like $_POST['table'] in PHP.

I tried the开发者_如何学Go following code, but I want the values as an array...

$("input[name='table[]']").each(function(){document.write($(this).val());});


var arrInputValues = new Array();
$("input[name='table\\[\\]']").each(function(){
     arrInputValues.push($(this).val()); 
     // you can also use this
     //arrInputValues.push(this.value);
});

Now your arrInputValues contains all the value.

You can also use

$("input[name='table[]']").each(function(){

You can see a working demo

You can use join() method to join the values in the array.

arrInputValues.join(',');

will join the array elements as a string separated by ,.


Following code gives the value of each input

 var arr=new Array();
 $("input[name='table[]']").each(function()
 {  
    var val=$(this).attr('value');
    document.write(val);
    arr.Push(val);
 });


You need to escape the [] chars, try this:

$("input[name='table\\[\\]']").each(function()
 ...........


the [ and ] characters are special characters, you need to escape them

$("input[name='table\\[\\]']").each(function()
 {  document.write($(this).val());           });
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号