Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 通过其值检查无线电输入字段_Jquery_Html - Fatal编程技术网

Jquery 通过其值检查无线电输入字段

Jquery 通过其值检查无线电输入字段,jquery,html,Jquery,Html,我一直在寻找答案,但我所得到的一切都不能解决我的问题 我想像这样检查收音机输入 <input type="radio" name="field" value="a" /> <input type="radio" name="field" value="b" checked /> <input type="radio" name="field" value="c" /> <a href="javascript:;">cancel</a>

我一直在寻找答案,但我所得到的一切都不能解决我的问题

我想像这样
检查
收音机
输入

<input type="radio" name="field" value="a" />
<input type="radio" name="field" value="b" checked />
<input type="radio" name="field" value="c" />
<a href="javascript:;">cancel</a>

// jQuery
$("a").click(function(){
    var checked = "b";
    $('input').each(function(){
        var value = $(this).val();
        if(value == checked){
            $(this).attr('checked', true);
        } else {
            $(this).attr('checked', false);
        }
    });
});

//jQuery
$(“a”)。单击(函数(){
var checked=“b”;
$('input')。每个(函数(){
var值=$(this.val();
如果(值==选中){
$(this.attr('checked',true);
}否则{
$(this.attr('checked',false);
}
});
});
这就是我想要实现的,默认值是“b”,因此如果用户单击“a”或“c”,并希望在不单击“b”的情况下将值返回到“b”,然后单击链接,我希望选中值“b”,从而取消选中其他值。但是当我运行代码时,它只是取消选中所有的值。 如何使用jQuery实现这一点请尝试以下方法: 已编辑

JAVASCRIPT:

$("a").click(function(){
   $('input[name="field"][value="b"]').attr('checked', true);
});
HTML:

<input type="radio" name="field" value="a" />
<input type="radio" name="field" value="b" checked />
<input type="radio" name="field" value="c" />
<a href="javascript:;">cancel</a>

并非总是可以使用输入类型重置。因此,您首先需要:

  • 保存当前选中的无线电索引
  • 单击链接时设置此收音机:
$(函数(){
$(“a”).data('radio-checked-index',$('radio:checked').index('radio:radio'));
$(“a”)。单击(函数(){
var checkedIndex=+$(“a”).data('radio-checked-index');
$(':radio:eq('+checkedIndex+')).prop('checked',true);
});
});


是否将其包装在
表单中
?如果是,只需使用输入类型重置。否则,您可以使用:将
.attr
更改为
.prop
请参见:@Wolff,如果他在表单中有更多输入,那么将重置所有表单值,我猜他只想重置收音机框。我想您可能在某个地方遗漏了引号。您所说的
停止是什么意思?如果您再次单击它,此选中值不会重置为“b”