jQuery:按类别更新选择特定单选按钮时的标记?

时间:2012-01-16 18:05:51

标签: javascript jquery dynamic radio

如何更新标签类="能够"什么时候选择了一个特定的单选按钮?

步骤: 页面打开span类能够被清除。 无论何时选择radio1,radio2或radio3: 如果选择了radio1,则span class ="能够"使用输入host1的值更新。 如果选择了radio2,则span class ="能够"使用输入host2的值更新。 如果选择了radio3,则span class ="能够"使用输入host3的值更新。

提前谢谢!

    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    <title>jQuery Update span by radio button selection</title>
    <link rel="stylesheet" href="themes/base/jquery.ui.all.css">
    <script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
    <script src="js/jquery-1.6.2.min.js"></script>
    <script src="ui/jquery.ui.core.js"></script>
    <script src="ui/jquery.ui.datepicker.js"></script>
    <script>

    </script>
    </head>
    <body>

    <div>
    <input type="text" id="host1" value="Able">
    <input type="text" id="host2" value="Baker">
    <input type="text" id="host3" value="Charlie"><br>

    <label><input type="radio" name="radiogroup" value="yes" id="radio1" />Yes</label>
    <label><input type="radio" name="radiogroup" value="no"  id="radio2" />No</label>
    <label><input type="radio" name="radiogroup" value="maybe" id="radio3" />Maybe</label>
    <br>

    </div>

    <span id="result1" class="able"> </span><br>
    <span id="result2" class="able"> </span><br>
    <span id="result3" class="able"> </span>


    </body>
    </html>

2 个答案:

答案 0 :(得分:0)

您可能希望将单击事件添加到单选按钮,onclick会更改跨度值。

类似的东西:

$('#radio1').click(function(){$('span.able').html($('#host1').val());});

你也可以让它更常见:

$('input:radio').live('click',function(){
  var jThis = $(this), jText = $('#'+jThis.attr('text_id'));
  $('span.able').html(jText.val());
});

您需要在单选按钮中添加属性“text_id”,以标识应该为给定的单选按钮使用哪个文本。

答案 1 :(得分:0)

试试这个

$('input:radio').click(function(){

    var id = this.id.replace('radio', '');
    $('#result' + id).text($('#host' + id).val()); 

});

正在使用 Demo