Get the value of inputs using jQuery

I have 100 entries with a name table[]. How can I get their value using jQuery as an array?

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

I tried the following code, but I need the values ​​as an array ...

$("input[name='table[]']").each(function(){document.write($(this).val());});
+3
source share
4 answers
var arrInputValues = new Array();
$("input[name='table\\[\\]']").each(function(){
     arrInputValues.push($(this).val()); 
     // you can also use this
     //arrInputValues.push(this.value);
});

Your arrInputValues ​​values ​​now contain all the values.

You can also use

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

You can see a working demo.

You can use the join () method to combine values ​​into an array.

arrInputValues.join(',');

joins the elements of the array as a string separated by a character ,.

+5
source

The following code gives the value each entry

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

[] ,

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

[], :

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

Source: https://habr.com/ru/post/1735734/


All Articles