2013-03-07 86 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"); 
    } 
}); 
相关问题