jQuery jquery勾选复选框IE问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2525331/
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 13:42:26  来源:igfitidea点击:

jquery checked checkbox IE problem

jquery

提问by lidermin

I'm having an issue verifying if a checkbox is checked using jquery on Internet Explorer. This is the code I'm using:

我在验证是否在 Internet Explorer 上使用 jquery 选中复选框时遇到问题。这是我正在使用的代码:

if ($('#chkjq_1').attr('checked') == true)

It works fine on Firefox or Chrome, but on Internet Explorer 7, the condition is always false because the browser sets the property this way:

它在 Firefox 或 Chrome 上运行良好,但在 Internet Explorer 7 上,条件始终为 false,因为浏览器以这种方式设置属性:

<input id="chkjq_1" type="checkbox" CHECKED/> IE7

And the right way is the following: (Firefox, Chrome):

正确的方法如下:(Firefox,Chrome):

<input id="chkjq_1" type="checkbox" checked="checked"/> FF, Chrome, etc

What should I do to avoid this issue on Internet Explorer 7; is there a generic way in jquery to solve this?

我应该怎么做才能避免 Internet Explorer 7 上的这个问题;jquery 中有没有通用的方法来解决这个问题?

Thanks in advance.

提前致谢。

回答by Sarfraz

Try this:

尝试这个:

if ($('#chkjq_1').is(':checked'))
{
  // more code
}

回答by S. Rooks

The only thing I could get to work consistently was to test like this:

我唯一能持续工作的就是像这样测试:

if ($('#chkjq_1').is('[CHECKED]'))

Using all caps on the selector like IE renders it when you open Developer Tools. Works across browsers so far as I can tell. .is(':checked') wouldn't match for me (using JQuery 1.7.1). Tested using IE 7 and IE 8 in multiple modes. Other browsers (Firefox, Safari tested) seem not to care.

当您打开开发人员工具时,像 IE 一样在选择器上使用所有大写字母会呈现它。据我所知,可以跨浏览器工作。.is(':checked') 不适合我(使用 JQuery 1.7.1)。在多种模式下使用 IE 7 和 IE 8 进行测试。其他浏览器(Firefox、Safari 测试)似乎并不关心。

回答by user1353316

"change" event of checkbox in IE is triggered weirdly.

IE 中复选框的“更改”事件被奇怪地触发。

This can be solved as:- 1.) bind the onclick event of input checkbox to required target function to be fired 2.) in the target function, check the status of the checkbox by checking the "checked" attribute via-

这可以解决为:- 1.) 将输入复选框的 onclick 事件绑定到需要触发的目标函数 2.) 在目标函数中,通过检查“已检查”属性来检查复选框的状态 -

if ($('#chkBox').attr("checked") == "checked"){
      // do something ----or .attr("checked", "checked"); ---for another checkbox
}else{
      // do something else ----or .removeAttr("checked"); ---for another checkbox
}