jQuery .attr('checked','checked') 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5270689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:51:39  来源:igfitidea点击:

.attr('checked','checked') does not work

jquery

提问by Masao Liu

I am trying to check radio. Neither the following works:

我正在尝试检查收音机。以下都不起作用:

[edit]

[编辑]

$('selector').attr('checked','checked');
$('selector').attr('checked',true);

The two alert() in the following code show "1" and "a", respectively. My expectation is the second alert() showing "b".

以下代码中的两个 alert() 分别显示“1”和“a”。我的期望是第二个 alert() 显示“b”。

Opera does check the second radio box in its browser, but its element inspector, dragonfly, shows that the DOM is not changed.

Opera 确实检查了浏览器中的第二个单选框,但它的元素检查器蜻蜓显示 DOM 没有改变。

[end edit]

[结束编辑]

I had read the FAQ before I posted this question:

在发布这个问题之前,我已经阅读了常见问题解答:

http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_check.2Funcheck_a_checkbox_input_or_radio_button.3F

http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_check.2Funcheck_a_checkbox_input_or_radio_button.3F

Helps will be much appreciated!

帮助将不胜感激!

TIA

TIA

The xhtml page and code follows:

xhtml 页面和代码如下:

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Weird Behavior</title>
<script type="text/javascript" src="scripts/jquery.js"/>
<script type="text/javascript">
function clickme(){
    alert($('input[name="myname"][value="b"]').length);
    $('input[name="myname"][value="b"]').attr('checked','checked');
    $('#b').attr('checked',true);
    alert($('input[name="myname"][checked]').val());
};
</script>
</head>
<body>
    <form action="">
        <fieldset>
            <legend>Why check is not switched?</legend>
            <input type="radio" name="myname" value="a" id="a" checked="checked"/>A
            <input type="radio" name="myname" value="b" id="b"/>B
        </fieldset>
    </form>
    <p>
    <button type="button" onclick="clickme()">click me</button>
    </p>
</body>
</html>

采纳答案by Alex Wayne

With jQuery, never use inline onclickjavascript. Keep it unobtrusive. Do this instead, and remove the onclickcompletely.

使用 jQuery,永远不要使用内联onclickjavascript。保持低调。改为这样做,并onclick完全删除。

Also, note the use of the :checkedpseudo selector in the last line. The reason for this is because once the page is loaded, the html and the actual state of the form element can be different. Open a web inspector and you can click on the other radio button and the HTML will still show the first one is checked. The :checkedselector instead filters elements that are actually checked, regardless of what the html started as.

另外,请注意:checked最后一行中伪选择器的使用。这样做的原因是因为一旦页面被加载,html 和表单元素的实际状态可能不同。打开一个 web 检查器,你可以点击另一个单选按钮,HTML 仍然会显示第一个被选中。的:checked选择,而不是筛选掉去查看,无论什么样的HTML开始为元素。

$('button').click(function() {
    alert($('input[name="myname"][value="b"]').length);
    $('input[name="myname"][value="b"]').attr('checked','checked');
    $('#b').attr('checked',true);
    alert($('input[name="myname"]:checked').val());
});

http://jsfiddle.net/uL545/1/

http://jsfiddle.net/uL545/1/

回答by Fizer Khan

 $('.checkbox').attr('checked',true);

will not work with from jQuery 1.6.

不会与jQuery的1.6工作

Instead use

而是使用

$('.checkbox').prop('checked',true);
$('.checkbox').prop('checked',false);

Full explanation / examples and demo can be found in the jQuery API Documentation https://api.jquery.com/attr/

完整的解释/示例和演示可以在 jQuery API 文档 https://api.jquery.com/attr/ 中找到

回答by trace

$('.checkbox').prop('checked',true); 
$('.checkbox').prop('checked',false);

... works perfectly with jquery1.9.1

...与jquery1.9.1完美配合

回答by KingRider

Why not try IS?

为什么不试试

$('selector').is(':checked') /* result true or false */

Look a FAQ: jQuery .is()enjoin us ;-)

查看常见问题解答: jQuery .is()禁止我们 ;-)

回答by Felix Kling

It works, but

它有效,但是

$('input[name="myname"][checked]').val()

will return the value of the first elementwith attribute checked. And the aradio button still has this attribute (and it comes before the bbutton). Selecting bdoes not removethe checkedattribute from a.

将返回第一个具有属性的元素的值checked。并且a单选按钮仍然具有此属性(并且它出现在b按钮之前)。选择b删除checked从属性a

You can use jQuery's :checked:

您可以使用 jQuery 的:checked

$('input[name="myname"]:checked').val()

DEMO

演示



Further notes:

补充说明:

  • Using $('b').attr('checked',true);is enough.
  • As others mentioned, don't use inline event handlers, use jQuery to add the event handler.
  • 使用$('b').attr('checked',true);就够了。
  • 正如其他人提到的,不要使用内联事件处理程序,使用 jQuery 添加事件处理程序。

回答by Ed Fryed

$("input:radio").attr("checked",true); //for all radio inputs

or

或者

$("your id or class here").attr("checked",true); //for unique radios

equally the same works for ("checked","checked")

同样适用于 ("checked","checked")

回答by RubyFanatic

I don't think you can call

我不认为你可以打电话

$.attr('checked',true);

because there is no element selector in the first place. $ must be followed by $('selector_name'). GOod luck!

因为首先没有元素选择器。$ 后面必须跟有 $('selector_name')。祝你好运!