2009-09-23 63 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>