2013-03-07 87 views
1

有人可以給我一個簡單的jQuery代碼的幫助,當單擊不同的無線電Bottom時,然後顯示不同的內容。
http://jsfiddle.net/AXsVY/當選擇單選按鈕時jQuery改變內容

HTML

<label class="radio inline"> 
    <input id="up_radio" type="radio" name="optionsRadios" value="update" checked> 
    Update 
</label> 
<label class="radio inline"> 
    <input id="ov_radio" type="radio" name="optionsRadios" value="overwritten"> 
    Overwritten 
</label> 

<p id="cont"> 
    sth. here 
</p> 

的JavaScript

$("#up_radio").click(function(){ 

    if ($("#up_radio").is(":checked")) { 

     //change to "show update" 
     $("#cont").value = "show update"; 

    } else if ($("#ov_radio").is(":checked")) { 

     // change to "show overwritten" 
    } 
}); 

回答

11

http://jsfiddle.net/AXsVY/2/

上變化不是點擊使用。點擊觸發器會更改,但用戶也可以選中並點擊箭頭鍵。它將始終爲change

$('input[name="optionsRadios"]').on('change', function(){ 
    if ($(this).val()=='update') { 
     //change to "show update" 
     $("#cont").text("show update"); 
    } else { 
     $("#cont").text("show Overwritten"); 
    } 
}); 

而且,正確的語法設定值$("#something").val("the value");但在這種情況下#cont是一個div,所以你需要使用.text().html()

+0

+1包括小提琴 – mfeingold 2013-03-07 02:38:44

0
$("input:radio[name=optionsRadios]").click(function() { 
     var value = $(this).val(); 

     if(value="update") 
      $("#cont").text("show update"); 
     else 
     $("#cont").text("show update other"); 

    }); 
2

解決,根據你的方法.. 有更多的優化,你可以這樣做,但我離開它主要是作爲-是:

http://jsfiddle.net/digitalextremist/mqrHs/

這裏有一個小提琴以外的代碼:

$(".radio input[type='radio']").on('click', function(){ 
    if ($("#up_radio").is(":checked")) { 
     $("#cont").html("show update"); 
    } else if ($("#ov_radio").is(":checked")) { 
     $("#cont").html("show overwritten"); 
    } 
}); 
相關問題