如何在jQuery中重置单选按钮,以便不进行任何检查 [英] How to reset radiobuttons in jQuery so that none is checked

查看:365
本文介绍了如何在jQuery中重置单选按钮,以便不进行任何检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在HTML中有如下单选按钮:

I have radio buttons in HTML like this:

<td>
    <input id="radio1" type="radio" name="correctAnswer" value="1">1</input>
    <input id="radio2" type="radio" name="correctAnswer" value="2">2</input>
    <input id="radio3" type="radio" name="correctAnswer" value="3">3</input>
    <input id="radio4" type="radio" name="correctAnswer" value="4">4</input>
</td>

这是在表单标签中,当用户提交表单时,我想制作所有单选按钮返回默认值。

This is in a form tag, and when user submits a form I want to make all the radio buttons back to the default. Meaning none of them checked.

我有这段代码,但是它给出了一个错误,指出 [0]为空或不是对象

I have this code but it gives an error saying [0] is null or not an object

$('input[@name="correctAnswer"]')[0].checked = false;
$('input[@name="correctAnswer"]')[1].checked = false;
$('input[@name="correctAnswer"]')[2].checked = false;
$('input[@name="correctAnswer"]')[3].checked = false;

我正在IE 6中执行此操作。

I am doing this in IE 6.

推荐答案

在jQuery 1.6之前的版本中使用:

In versions of jQuery before 1.6 use:

$('input[name="correctAnswer"]').attr('checked', false);

在1.6之后的jQuery版本中,您应该使用:

In versions of jQuery after 1.6 you should use:

$('input[name="correctAnswer"]').prop('checked', false);

但是如果您使用的是1.6.1+,则可以使用第一种形式(请参见下面的注释2) 。

but if you are using 1.6.1+ you can use the first form (see note 2 below).

注1:重要的是,第二个参数为 false 而不是 false ,因为并非虚假值。即

Note 1: it is important that the second argument be false and not "false" since "false" is not a falsy value. i.e.

if ("false") {
    alert("Truthy value. You will see an alert");
}

注2:从jQuery 1.6.0开始,现在有两种相似的方法, .attr .prop 可以完成两个相关但略有不同的事情。在这种情况下,如果您使用1.6.1+,则上述建议可以奏效。以上内容不适用于1.6.0,如果您使用1.6.0,则应升级。如果您需要详细信息,请继续阅读。

Note 2: As of jQuery 1.6.0, there are now two similar methods, .attr and .prop that do two related but slightly different things. If in this particular case, the advice provide above works if you use 1.6.1+. The above will not work with 1.6.0, if you are using 1.6.0, you should upgrade. If you want the details, keep reading.

详细信息::使用直接HTML DOM元素时,DOM元素附有一些属性( 选中的类型等)提供了与HTML页面运行状态的界面。还有 .getAttribute / .setAttribute 接口,该接口提供对HTML中提供的HTML属性值的访问。在1.6之前的版本中,jQuery通过提供一种访问两种类型的值的方法 .attr 来模糊区分。 jQuery 1.6+提供了两种方法 .attr .prop 来区分这两种情况。

Details: When working with straight HTML DOM elements, there are properties attached to the DOM element (checked, type, value, etc) which provide an interface to the running state of the HTML page. There is also the .getAttribute/.setAttribute interface which provides access to the HTML Attribute values as provided in the HTML. Before 1.6 jQuery blurred the distinction by providing one method, .attr, to access both types of values. jQuery 1.6+ provides two methods, .attr and .prop to get distinguish between these situations.

.prop 允许您在DOM元素上设置属性,而 .attr 允许您设置HTML属性值。如果您使用纯DOM,并将选中的属性 elem.checked 设置为 true false 更改运行值(用户看到的内容),返回的值将跟踪页面状态。 elem.getAttribute('checked')但是仅返回初始状态(并返回'checked'未定义,具体取决于HTML的初始状态)。在1.6.1+中,使用 .attr('checked',false)都执行 elem.removeAttribute('checked') elem.checked = false ,因为此更改引起了许多向后兼容性问题,并且无法真正确定您是否要设置HTML属性或DOM属性。请参阅 .prop的文档中的详细信息。

.prop allows you to set a property on a DOM element, while .attr allows you to set an HTML attribute value. If you are working with plain DOM and set the checked property, elem.checked, to true or false you change the running value (what the user sees) and the value returned tracks the on page state. elem.getAttribute('checked') however only returns the initial state (and returns 'checked' or undefined depending on the initial state from the HTML). In 1.6.1+ using .attr('checked', false) does both elem.removeAttribute('checked') and elem.checked = false since the change caused a lot of backwards compatibility issues and it can't really tell if you wanted to set the HTML attribute or the DOM property. See more information in the documentation for .prop.

这篇关于如何在jQuery中重置单选按钮,以便不进行任何检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆