Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery accessing form array
    text
    copied!<p>I have a php form and I want to display or hide certain fields dependant on what I select in a drop down. I have code below which works:</p> <pre><code>$(document).ready(function() { $('div.pupname').hide(); $('div.pupnid').hide(); if ($("#perpetrator").val == "1") { $('div.pupname').hide(); $('div.pupid').show(); else { $('div.pupid').hide(); $('div.pupname').show(); } $("#perpetrator").change(function() { if ($("#perpetrator").val() == "1") { $('div.pupname').hide(); $('div.pupid').show(); else { $('div.pupid').hide(); $('div.pupname').show(); } }); }); </code></pre> <p>I now want to have a multi row block which I am generating using php so I can generate the divs as <em>pupid1</em>, <em>pupid2</em> etc. and the field <code>$m_perpetrator</code> as an array. I've tried a few ways of accessing the array but can't get anything to work. I thought he following might be along the correct lines for accessing the elements of the array but it does nothing. </p> <pre><code>if ($("#m_perpetrator[1]").val == "1") { $('div.pupname1').show(); $('div.pupid1').hide(); } else { $('div.pupid1').show(); $('div.pupname1').hide(); } </code></pre> <p>Added</p> <p>Here is a snippet of the HTML</p> <pre><code>&lt;td&gt;&lt;select name="m_perpetrator[1]" id="m_perpetrator[1]"&gt; &lt;option value="1"&gt;Current&lt;/option&gt; &lt;option value="2" selected&gt;Former&lt;/option&gt; &lt;option value="3"&gt;Parent/Carer&lt;/option&gt; &lt;/select&gt; &lt;/td&gt; &lt;td&gt;Name&lt;/td&gt; &lt;td&gt; &lt;div class="pupname1"&gt; &lt;input type="text" size=60 name="m_pupil_name[1]" value="JOHN SMITH"&gt; &lt;/div&gt; &lt;div class="pupid1"&gt; &lt;select name="m_pupil_id[1]" onchange="getXtras(this)"&gt; &lt;option value=""&gt;Select .....&lt;/option&gt; </code></pre> <p>So basically this is a multi row form and on each row if Current is selected then I want to display pupil_id if anything else is selected then I want to display pupil name. For processing the input I run round the array m_perpetrator[] using php Jim</p>
 

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