【问题标题】:Get value of checked radio button [duplicate]获取选中的单选按钮的值[重复]
【发布时间】:2016-07-13 09:14:14
【问题描述】:
<input type="radio" id="radio1" name="radiobuttons" value="100">
<input type="radio" id="radio2" name="radiobuttons" value="200">
<input type="radio" id="radio3" name="radiobuttons" value="300">


<div id="selectedvalue"></div>

如何获取选中的单选按钮的值并将其输出为 "selectedvalue" 内的文本?该函数应在页面加载和输入更改时采用选中单选的值。

【问题讨论】:

  • 请谷歌你的标题

标签: jquery html


【解决方案1】:
$(document).ready(function () {
    //apply the value on page ready (you probably don't have to wait until everyithing loads - otherwise chage it to window.load()
    $('#selectedvalue').html( $('input[name=radiobuttons]:checked').val() );

    //bind the change event to update the element
    $('input[name=radiobuttons]').on('change', function () {
        $('#selectedvalue').html( $(this).val() );
    });
});

【讨论】:

    【解决方案2】:

    您可以通过更改 selectedvalue div 的文本来做到这一点。

    更改 div 的 HTML 可能没有必要,因为您只想显示一个数字

    $('#selectedvalue').text($('input[name=radiobuttons]:checked').val());
    

    API -

    【讨论】:

    • 这会在页面加载和输入更改时输出值?
    • 那就把它放在一个简单的事件里。
    猜你喜欢
    • 2017-12-20
    • 2016-06-02
    • 2012-07-18
    • 2013-10-15
    • 2016-11-09
    • 2016-12-11
    • 1970-01-01
    • 2016-09-20
    相关资源
    最近更新 更多