jQuery corresponding radio button selection
I have some divs, each with a radio button and a drop-down box. What is some simple jQuery to, on selection of the drop-down box, check the radio button within the same div?
<div id="x">
<%= radio_button_tag "opt", "gene", false %>
<%= select_开发者_JS百科tag "opt", options_for_select(@genes), { :multiple => false, :class => 'selectors'} %>
</div><br/>
If your HTML looks something like this:
<div class="some-class">
<input id="r1" type="radio" name="radios">
<label for="r1">Radio me</label>
<select>
<option>one</option>
<option>two</option>
<option>three</option>
</select>
</div>
<div class="some-class">
<input id="r2" type="radio" name="radios">
<label for="r2">Radio me</label>
<select>
<option>four</option>
<option>five</option>
<option>six</option>
</select>
</div>
<div class="some-class">
<input id="r3" type="radio" name="radios">
<label for="r3">Radio me</label>
<select>
<option>seven</option>
<option>eight</option>
<option>nine</option>
</select>
</div>
then your JavaScript could look something like this:
$('div.some-class > select').live('change', function ()
{
$(this).prevAll('input:radio').attr('checked', true);
});
http://jsfiddle.net/mattball/CBGMD/
$('select').bind('click', function() {
$(this).parent('div').children('input[type=radio]').attr('checked','checked');
});
$('.selectors').change(function() {
$(this).prev().attr('checked', true);
});
精彩评论