简体   繁体   English

更改单选按钮上的div文本单击

[英]change div text on radion button click

I want to put the radio button value in the status div. 我想将单选按钮值放在状态div中。 The text of status div should change, according to the radio button selected by the user. 状态div的文本应根据用户选择的单选按钮进行更改。 The code that I used bellow is not working. 我在下面使用的代码无法正常工作。 Please help. 请帮忙。 Thanks! 谢谢!

HTML code: HTML代码:

 <form action="">
     <input type="radio" name="sex" value="male">Male<br>
     <input type="radio" name="sex" value="female">Female<br>
     <div id="status"></div>
  </form>​

JS code: JS代码:

 $(document).ready(function () {
    RadioStatus="";

    $("input[type='radio']:checked").each( function()
    { 
        if ($(this).attr('checked');)
              RadioStatus=$(this).val();
              $("#status").text(RadioStatus);
        });

    $("input[type='radio']").change(function()  
     {
             RadioStatus= $('input[type='radio']:checked').val()
            $('#status').text(RadioStatus);        
    });
});

This should work: 这应该工作:

$("input[type='radio']").click(function() {
    $('#status').text($(this).val());
});

A few minor adjustments to your code got it to work just fine: 对您的代码进行一些细微调整,使其可以正常工作:

$(document).ready(function () {
    RadioStatus="";

    $("input[type='radio']:checked").each( function()
    { 
        if ($(this).attr('checked'))
              RadioStatus=$(this).val();
              $("#status").text(RadioStatus);
        });

    $("input[type='radio']").change(function()  
     {
             RadioStatus= $('input[type="radio"]:checked').val();
            $('#status').text(RadioStatus);        
    });
});​

see: http://jsfiddle.net/f6Tru/ 请参阅: http//jsfiddle.net/f6Tru/

..or you can really simplify it by using techfoobar's: ..或者你真的可以通过使用techfoobar的简化一下:

$(document).ready(function () {
    $("input[type='radio']").click(function() {
        $('#status').text($(this).val());
    });
});​

instead. 代替。 see: http://jsfiddle.net/RrhYM/ 参见: http : //jsfiddle.net/RrhYM/

You have a problem in your single quotes ' 您在单引号中有问题'

Change: 更改:

RadioStatus= $('input[type='radio']:checked').val()

To: 至:

RadioStatus= $('input[type="radio"]:checked').val()

Try this code 试试这个代码

$('input[name ^=sex]').click(function() {
        $('#status').text($(this).val());
});

Simply use change to determine that a radio button is selected. 只需使用change来确定已选中单选按钮。 Then use html or text to append the value of chosen radio button to your <div id="status"> . 然后使用htmltext将所选单选按钮的值附加到<div id="status">

$(document).ready(function () {
    $('input[type=radio]').on("change", function() {
        $('#status').html($(this).val());
        // Alternative
        // $('#status').text($(this).val());
    });
});​

DEMO 演示

demo 演示

$("input:radio").click(function() { //use $("input:radio[name='sex']").click if more radio buttons there
  $('#status').text($(this).val());
});​

You may achieve that with the following JS code : 您可以使用以下JS代码实现这一目标:

$("input").click(function(e){
     var selectedValue = e.currentTarget.value;                 
    $('#status').html(selectedValue );       
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM