重置js中的单选按钮和复选框值
Reset radio button and checkbox value in js
我有一组单选按钮,其中一个是这样选中的。
<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>
相关文章:
- 复选框/单选按钮-添加所选项目的总价
- 将文本框/单选按钮/和下拉列表中的信息添加到dataTable
- 如何防止jQuery中复选框单击时执行多个代码
- 如果用户未选中复选框或选择按钮,如何为复选框或单选按钮设置一些默认值
- 如何在更改节点 JS 表中的复选框时禁用按钮
- 如何使用 rails3-jquery-autocomplete 更新复选框/单选按钮
- IE issue-使用复选框/单选按钮时,jQuery.change()不会触发回调
- 引导复选框/单选按钮不改变<输入>价值
- 在复选框单击中,更改可以单击的按钮
- 样式复选框&单选按钮
- 启用两个提交按钮上的复选框单击
- jQuery缓存刷新复选框单选按钮状态
- 基于复选框单选按钮调整总成本标价
- 自定义复选框/单选按钮,不用ID和FOR,也不用angular.js
- 单击复选框以禁用按钮
- 表单提交的复选框:点击按钮时显示错误
- 我如何有一个复选框/单选按钮张贴到一个文本区,当我点击提交按钮
- 如果我们在鼠标下移时设置preventDefault,然后双击文档,那么它就会阻止复选框/单选标签的默认行为
- Ipad复选框/单选按钮操作缓慢
- 如何在 JavaScript 中创建带有 3 个复选框/单选按钮作为选项的提示