jQuery对应的单选按钮选择

时间:2011-03-24 15:08:49

标签: jquery html

我有一些div,每个div都有一个单选按钮和一个下拉框。什么是简单的jQuery,在选择下拉框时,检查同一个div中的单选按钮?

<div id="x">
  <%= radio_button_tag "opt", "gene", false %>
  <%= select_tag "opt", options_for_select(@genes), { :multiple => false, :class => 'selectors'} %>
</div><br/>

3 个答案:

答案 0 :(得分:3)

如果您的HTML看起来像这样:

<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>

然后你的JavaScript看起来像这样:

$('div.some-class > select').live('change', function ()
{
    $(this).prevAll('input:radio').attr('checked', true);
});

http://jsfiddle.net/mattball/CBGMD/

答案 1 :(得分:0)

$('select').bind('click', function() {
    $(this).parent('div').children('input[type=radio]').attr('checked','checked');
});

答案 2 :(得分:0)

$('.selectors').change(function() {
    $(this).prev().attr('checked', true);
});