Note that there are some explanatory texts on larger screens.

plurals
  1. POObtain values of array with jquery
    primarykey
    data
    text
    <p>Let's say i have this form:</p> <pre><code> &lt;form method="post" name="b" &gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;Field A&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='field[0][a]' id='field[0][a]'&gt;&lt;/td&gt; &lt;td&gt;Field B&lt;/td&gt; &lt;td&gt;&lt;textarea name='field[0][b]' id='field[0][b]'&gt;&lt;/textarea&gt;&lt;/td&gt; &lt;td&gt;&lt;button&gt;remove&lt;/button&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;div id="container"&gt;&lt;/div&gt; &lt;button id="mybutton"&gt;add form&lt;/button&gt; &lt;div align="center"&gt; &lt;p&gt;&lt;button onClick="dadosFormulario()" value="Registar" name="registar"&gt;Registo&lt;/button&gt;&lt;/p&gt; &lt;/div&gt; &lt;/form&gt; </code></pre> <p>and i have a jquery function that duplicates the inputs i have when i click the "add form" button. So, regarding that, i'm trying to save the values of the inputs with the .val() method of jquery.</p> <p>So, if i have an input like this one:</p> <pre><code>&lt;input type='text' name='teste' id='teste'&gt; </code></pre> <p>i know that with this:</p> <pre><code>var v = $('#teste').val(); </code></pre> <p>i can receive her value. However, if i have an array of inputs, and that's my situation since i can duplicate the fields of the form, how can i obtain the multiple values from the following inputs?</p> <pre><code>&lt;input type='text' name='field[0][a]' id='field[0][a]'&gt; &lt;input type='text' name='field[0][b]' id='field[0][b]'&gt; </code></pre> <p>var v = $('#field[iterator][a]').val(); won't work, it results in 'undefined'.</p> <p>jquery function where i can't get the values from the inputs with the val(); gives undefined:</p> <pre><code>$countForms = 1; (function($){ $.fn.addForms = function(idform){ var myform = "&lt;table&gt;"+ " &lt;tr&gt;"+ " &lt;td&gt;Field A ("+$countForms+"):&lt;/td&gt;"+ " &lt;td&gt;&lt;input type='text' name='field\\["+$countForms+"\\]\\[a\\]'&gt;&lt;/td&gt;"+ " &lt;td&gt;Field B ("+$countForms+"):&lt;/td&gt;"+ " &lt;td&gt;&lt;textarea name='field["+$countForms+"]['b']'&gt;&lt;/textarea&gt;&lt;/td&gt;"+ " &lt;td&gt;&lt;button&gt;remove&lt;/button&gt;&lt;/td&gt;"+ " &lt;/tr&gt;"+ "&lt;/table&gt;"; if(idform=='mybutton'){ myform = $(myform); $("button", $(myform)).click(function(){ $(this).parent().parent().remove(); }); $(this).append(myform); $countForms++; } }; })(jQuery); $(function(){ $("#mybutton").bind("click", function(e){ e.preventDefault(); var idform=this.id; if($countForms&lt;3){ $("#container").addForms(idform); } }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload