Note that there are some explanatory texts on larger screens.

plurals
  1. POAdvanced use of jQuery serializeArray()
    primarykey
    data
    text
    <p>I'm currently submitting a form via ajax and pass in a manually created data variable that looks a bit like this:</p> <pre><code>var data = 'answer1=' + $("input[name=question_1]:checked").val() + '&amp;q1_option=' + $("input[name=question_1]:checked").attr("id") </code></pre> <p>This continues throughout the list of form elements obviously increasing incrementally question_2, question_3 etc. The problem is that this is a bit messy. I'd like to use jQuery serializeArray but, to do this I would need to pass in an extra parameter. I need to pass the input value and the input id as this id is used in the data.</p> <p>Is there a way I can achieve this using jQuery serializeArray()?</p> <p>Example form markup:</p> <pre><code>&lt;label&gt;What is your gender?&lt;span class="required"&gt; *&lt;/span&gt;&lt;/label&gt; &lt;input id="1" type="radio" name="question_1" value="Male"&gt;&lt;label class="standard" for="1"&gt;Male&lt;/label&gt;&lt;br&gt; &lt;input id="2" type="radio" name="question_1" value="Female"&gt;&lt;label class="standard" for="2"&gt;Female&lt;/label&gt;&lt;br&gt; &lt;label&gt;How old are you?&lt;span class="required"&gt; *&lt;/span&gt;&lt;/label&gt; &lt;input id="3" type="radio" name="question_2" value="Under 25"&gt;&lt;label class="standard" for="3"&gt;Under 25&lt;/label&lt;br&gt; &lt;input id="4" type="radio" name="question_2" value="25-29"&gt;&lt;label class="standard" for="4"&gt;25-29&lt;/label&gt;&lt;br&gt; &lt;input id="5" type="radio" name="question_2" value="30-39"&gt;&lt;label class="standard" for="5"&gt;30-39&lt;/label&gt;&lt;br&gt; &lt;input id="6" type="radio" name="question_2" value="40-49"&gt;&lt;label class="standard" for="6"&gt;40-49&lt;/label&gt;&lt;br&gt; &lt;input id="7" type="radio" name="question_2" value="50-59"&gt;&lt;label class="standard" for="7"&gt;50-59&lt;/label&gt;&lt;br&gt; &lt;input id="8" type="radio" name="question_2" value="60+"&gt;&lt;label class="standard" for="8"&gt;60+&lt;/label&gt;&lt;br&gt; </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