使用 jQuery 重置单选按钮

2022-01-21 00:00:00 radio-button jquery javascript html

我有多个单选按钮.随机检查或选择单选按钮值后,我需要在单击按钮时将单选按钮重置为默认选中或选择的值.我的 HTML 代码如下.

I'm have multiple radio button. After check or select radio button value randomly, i need to reset radio button to default checked or selected value on button click. My HTML code is below.

function ResetRdo() {
    // Code to reset ?
}

HTML

<table>
    <tr>
        <td>
            <input type="radio" name="rdo1" value="a" checked="checked" />
            A
            <input type="radio" name="rdo1" value="b" />
            B
            <input type="radio" name="rdo1" value="c" />
            C
        </td>
    </tr>
    <tr>
        <td>
            <input type="radio" name="rdo2" value="1" />
            1
            <input type="radio" name="rdo2" value="2" checked="checked" />
            2
            <input type="radio" name="rdo2" value="3" />
            3
        </td>
    </tr>
    <tr>
        <td>
            <input type="radio" name="rdo3" value="1a" />
            1a
            <input type="radio" name="rdo3" value="2b"  />
            2b
            <input type="radio" name="rdo3" value="3c" checked="checked" />
            3c
        </td>
    </tr>
    <tr>
        <td>
            <input type="button" value="Reset" onclick="ResetRdo()" />
        </td>
    </tr>
</table>

推荐答案

$('input[type=radio]').prop('checked', function () {
    return this.getAttribute('checked') == 'checked';
});

这里是小提琴:http://jsfiddle.net/DNUdv/

相关文章