最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery change content when radio button selected - Stack Overflow

programmeradmin0浏览0评论

can someone give me help on a simple jQuery code that when clicked different radio bottuon then display the different content.
/

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"
    }
});

can someone give me help on a simple jQuery code that when clicked different radio bottuon then display the different content.
http://jsfiddle.net/AXsVY/

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"
    }
});
Share Improve this question asked Mar 7, 2013 at 2:33 Paul YinPaul Yin 1,7592 gold badges13 silver badges20 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 13

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

Use on change not on click. Click triggers change, but the user could also tab across and hit an arrow key. It will always change.

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

Also, the correct syntax to set a value is $("#something").val("the value"); but in this case #cont is a div, so you need to use .text() or .html().

Solved, based on your approach.. There are more optimizations you could do, but I left it mostly as-is:

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

Here's the code outside a fiddle:

$(".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" );
    }
});
   $("input:radio[name=optionsRadios]").click(function() {
        var value = $(this).val();

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

    });
发布评论

评论列表(0)

  1. 暂无评论