2009-09-23 61 views
0

我的页面上有3个单选按钮。在同一页面上也有3个按钮。我想在点击每个按钮时显示不同的按钮。捕获RadioButton单击并采取行动

<input id="Radio1" type="radio" name="grp1" class="abc" /> 
<input id="Radio2" type="radio" name="grp1" class="abc" /> 
<input id="Radio3" type="radio" name="grp1" class="abc" /> 

更新:只有一个按钮以显示对应于每个无线电button..For例如:如果收音机1被点击显示按钮1,如果收音机2被点击显示按钮2等

回答

3
$(function() { // at dom ready 
    $('input.abc').change(function() { // on radio's change event 
    $('.buttons-to-hide-class').hide(); // hide buttons 
    $('#button-to-show-id').show(); // show desired button 
    }); 
}); 
+0

只有一个按钮,显示对应于每个无线电button..For例如:如果收音机1单击显示按钮1,如果电台2点击Show按钮2,依此类推 – KJai 2009-09-23 07:58:16

1
$('#Radio1').click(function() { $('#button1').value = 'new value'; }); 

类似的东西

+0

我想这就是答案OP实际上是在寻找;而不是创建3个按钮,创建1并根据所选的单选按钮更改操作。 – beggs 2009-09-23 08:19:41

1

我认为尼克德Maeyer是在暗示比较好,而且我敢肯定有一个更好的方式来这样的代码,但:

脚本

$(function() { 
    //hide all the buttons when the page loads 
    $('.def').hide(); 
    //add an onchange function to each radiobutton 
    $('input.abc').change(function() { 
     //hide all the other buttons 
     $('.def').hide(); 
     //construct the ID of the button to show and show it 
     $('#' + this.id + '-Button').show() 
     }); 
    }); 

HTML

<input id="Radio1" type="radio" name="grp1" class="abc" /> 
<input id="Radio2" type="radio" name="grp1" class="abc" /> 
<input id="Radio3" type="radio" name="grp1" class="abc" /> 

<input id="Radio1-Button" type="button" value="1" class="def" ></input> 
<input id="Radio2-Button" type="button" value="2" class="def" ></input> 
<input id="Radio3-Button" type="button" value="3" class="def" ></input>