I have a issue with radio button array. I have 1 timer when timer is 0 and select 1 radio button then that value get in jquery.
<li>
<span class="option">Option1 : Value1 </span>
<input type="radio" name="cmbans[][1]" id="cmbans[][1]" value="Value1" />
</li>
<li>
<span class="option">Option2 : Value2 </span>
<input type="radio" name="cmbans[][2]" id="cmbans[][2]" value="Value2" />
</li>
<li>
<span class="option">Option1 : Value1 </span>
<input type="radio" name="cmbans[][3]" id="cmbans[][3]" value="Value3" />
</li>
This radio button name or id get in jquery but problem is this is array so how can i get this. Pls help me.
Thanks in advance
I'm not sure what your problem is but you can't define an associative array using your inputs name attribute.
Try
<input type="radio" name="cmbans[]" id="cmbans_1" value="Value2" />
<input type="radio" name="cmbans[]" id="cmbans_2" value="Value2" />
You can select using jquery all elements with id starts with cmbans to get an array of radio buttons. When the form is submitted your webserver will parse the inputs names into an array.
To select element whose ids' have special characters you have to escape these special characters. In your case [
and ]
, also your choice in id naming is quite stramge
$("#cmbans\\[\\]\\[1\\]").val()
I didn't get ypur problem correctly. I'm not sure if this one helps you..
Change your html like this..
<li>
<span class="option">Option1 : Value1 </span>
<input type="radio" name="cmbans[]" id="cmbans1" value="Value1" />
</li>
<li>
<span class="option">Option2 : Value2 </span>
<input type="radio" name="cmbans[]" id="cmbans2" value="Value2" />
</li>
<li>
<span class="option">Option1 : Value1 </span>
<input type="radio" name="cmbans[]" id="cmbans3" value="Value3" />
</li>
Now create jquery to get the value like this..
if(timer == 0){
var value = $('#id of radio').val();
}
Id of each and every HTML element must be unique within the HTML document.
you can define HTML array of elements in following manner
e.g
And for accessing html array elements using jquery assign same class to all elements within that html array and then we will iterate through elements of HTML array on basis of class
e.g $('.xyz').each(function() { alert($(this)); });
Reading your question I didn't get much about what exactly you want but take a shot with following. PHP looks like:
$str = ''
for(i = 0:i < 4;i++){
str.= '<li>';
str.= '<span class="option">Option1 : Value1 </span>';
str.= '<input type="radio" name="cmbans" value="'.cmbans[][i].'" />';
str.= '</li>';
}
echo str;
HTML Looks like:
<li>
<span class="option">Option1 : Value1 </span>
<input type="radio" name="cmbans" value="Value1" />
</li>
<li>
<span class="option">Option1 : Value1 </span>
<input type="radio" name="cmbans" value="Value2" />
</li>
<li>
<span class="option">Option1 : Value1 </span>
<input type="radio" name="cmbans" value="Value3" />
</li>
<li>
<span class="option">Option1 : Value1 </span>
<input type="radio" name="cmbans" value="Value4" />
</li>
And Jquery Looks like:
if(timer == 0){
var _value = $('input:radio:checked').val();
//do something with _Value.
}
I hope this is what you want...