How to get textbox array value using javascript / jquery

I have an array of text inputs like this:

<input type="textbox" name="mobileno[]"><br>
<input type="textbox" name="mobileno[]"><br>
<input type="textbox" name="mobileno[]"><br>

They are generated at runtime using the add more button.

Is there a way I can use jQuery to extract the value of the input text and pass it in an ajax request?

I created an ajax request, but I cannot get the value of the text input array in the template name = value.

+5
source share
4 answers

You can do the following:

$('input[name="mobileno[]"]').each(function(){
  alert($(this).val());
});

^=finds elements whose name begins with text mobileno , and then is eachused to cycle through all the elements to get their value.

Additional Information:

+5

-: " [es]". HTML , . , , . HTML. (PHP , ).

: type="textbox" . type="text". , , "text" , , , textbox.

vales name = value ( &), jQuery serialize :

alert($("#your-form-id").serialize());

http://api.jquery.com/serialize/

+1

:

var text= new Array();
$("input[name='mobileno[]']").each(function(){
    text.push($(this).val());
});  

If u warns variable text, you will get comma delimited output.

+1
source
var data = $('input[name="mobileno[]"]').map(function(){
  return this.name + '=' + this.value;
}).get().join('&');
alert(data);

it will give you something like mobileno[]=value&mobileno[]=value&mobileno[]=value. Then you can pass it to your ajax request.

+1
source

All Articles