IE未检测到jquery更改方法的复选框 [英] IE not detecting jquery change method for checkbox

查看:157
本文介绍了IE未检测到jquery更改方法的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码适用于FF,Safari,Chrome。但IE正在给我的问题。

The code below works in FF, Safari, Chrome. But IE is giving me issues.

当选中复选框时,我无法让IE检测到。

When a checkbox is checked, I cannot get IE to detect it.

$("#checkbox_ID").change(function(){

   if($('#'+$(this).attr("id")).is(':checked'))
   { 
       var value = "1"; 
   }
   else
   { 
       var value = "0"; 
   } 
   alert(value);

   return false;
});

很简单,我没有得到预期的弹出式窗口。

我甚至尝试这样:

Simply, I'm not getting that alert popup, as expected.
I've even tried it this way:

$("#checkbox_ID").change(function(){

   if( $('#'+$(this).attr("id")'+:checked').attr('checked',false))
   { 
       var value = "1"; 
   } 
   else
   { 
       var value = "0"; 
   } 
   alert(value);

   return false;
});

这里是简单的复选框输入:< input class =prodtype =checkboxname =checkbox_IDid =checkbox_IDvalue =1/>

Here's the simple checkbox input: <input class="prod" type="checkbox" name="checkbox_ID" id="checkbox_ID" value="1"/>

任何人都知道IE需要不同的jQuery方法?或者是我的代码刚刚关闭?

Anyone know if IE requires a different jquery method? or is my code just off?

推荐答案

正如sblom注意到IE7和下面你需要的元素, fire,以后的版本的IE和其他浏览器不需要元素明确失去焦点,只需更改应该启动此事件(如我的下面的示例中所示)。

As noted by sblom with IE7 and below you need the element to blur for this action to fire, later versions of IE and other browsers don't need the element to explicitly lose focus, simply changing should fire this event (as shown in my below example).

我相信你的问题可能是你在超出范围时尝试访问value变量。

I believe that your problem could be that you're trying to access the 'value' variable when it is out of scope.

$("#checkbox_ID").change(function(){
var value;
if($(this).is(':checked'))
{ 
    value = "1"; 
}
else
{ 
    value = "0"; 
} 
alert(value);

return false; 
});

这是一个工作预览,注意,因为你在函数结束时返回false,它将复选框重置为之前的值,因为你已经取消了点击的效果。

Here is a working preview, notice that because you return false at the end of the function it resets the checkbox to its previous value, as you have canceled the effect of the click.

这篇关于IE未检测到jquery更改方法的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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