重置js中的单选按钮和复选框值

Reset radio button and checkbox value in js

本文关键字:复选框 单选按钮 js 重置      更新时间:2023-09-26

我有一组单选按钮,其中一个是这样选中的。

<input name="test_data" type="radio" value='1' id="test_data">
<input name="test_data" type="radio" value='2' id="test_data" checked="checked">
<input name="test_data" type="radio" value='3' id="test_data">
<input name="test_data" type="radio" value='4' id="test_data">

我有一个这样的链接

<a href="#" id="resetvalue">reset value</a>

如何重置用户单击resetvalue id时的单选按钮值?我没有使用任何形式。如果有表单和按钮,我们可以使用reset属性。

你可以这样做:

jQuery

$('#resetvalue').click(function() {
  $('input[name="test_data"]:nth-of-type(2)').prop('checked', true);
});

使用:nth-of-type()选择器将单选按钮重置为第二个值。

JSFiddle

就是这样做的https://jsfiddle.net/0vLmt3L5/7/

$('#resetvalue').click(function() {
  $('input[value="2"]').prop('checked', true);
});

这将把它设置回原来的选中单选按钮,是的,我拿了亨特的小提琴,只是添加了

this: $('input[value="2"]').prop('checked', true);

$('input[name="test_data"]').prop('checked', false);
$("#resetvalue").on("click",function(e){
    $("input[type='radio']").removeAttr("checked");
});

$( '#resetvalue' ).click(function() {
    $( '[name="test_data"].checked' ).prop('checked', false);
    // Set eq from 0 (first radio) to value you want
    $( '[name="test_data"]' ).eq(0).prop('checked', true);
    return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="test_data" type="radio" value='1' id="test_data">
    <input name="test_data" type="radio" value='2' id="test_data" checked="checked">
    <input name="test_data" type="radio" value='3' id="test_data">
    <input name="test_data" type="radio" value='4' id="test_data">
<a href="#" id="resetvalue">Reset</a>