feiyang10086 发表于 2012-03-06 10:29

jquery 选中checkbox兼容问题

jquery 选中checkbox兼容问题





javascriptjquery
今天在做一个checkbox选中功能的时候,在本地测试已经通过,但是把代码上传到服务器上测试却是功能不能正常工作!最后老大告诉我本地和线上的jquery插件不一致,虽然我知道jquery插件有浏览器兼容的问题,但是这个还是自己第一次遇到或者说是留意到这个问题,下面将我做的写下来,提供给大家学习




Html代码<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script src="lib/jquery-1.6.4.min.js" type="text/javascript"></script>
<!--script src="lib/jquery-1.4.4.js" type="text/javascript"></script-->
<script type="text/javascript">
$(document).ready(   
    function(){   
      $("#jianrong").click(sovle_jquery_virsion);   
      $("#test").click(function(){   
            //如果是1.4版本得到的数据时boolean类型的   
            //如果是1.6版本得到的结果是字符串   
            if(true == $("#show_img_title").attr("checked")){   
                alert("是字符串true");   
            }   
            if("checked" == $("#show_img_title").attr("checked")){   
                alert("是字符串checked!");   
            }   
      });   
    }   );   

//解决不同版jquery的问题,可以用下面的方式判断   
function sovle_jquery_virsion(){   
    if("checked" == $('#show_img_title', parent.document.body).attr("checked") || true == $('#show_img_title', parent.document.body).attr("checked")){   
      alert("多选框被选中!");   
    }else{   
      alert("多选框没有被选中!");   
    }   
}   

</script>
<title>无标题文档</title>
</head>
<input type="checkbox" id="show_img_title" style="vertical-align:-3px;"><label style="margin: 0 0 0 4px;" for="show_img_title">照片标题描述带入日记</label><br>
<button id="test">test</button><br>
<button id="jianrong">兼容性问题测试</button>
<body>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script src="lib/jquery-1.6.4.min.js" type="text/javascript"></script>
<!--script src="lib/jquery-1.4.4.js" type="text/javascript"></script-->
<script type="text/javascript">
$(document).ready(
        function(){
                $("#jianrong").click(sovle_jquery_virsion);
                $("#test").click(function(){//如果是1.4版本得到的数据时boolean类型的
                        //如果是1.6版本得到的结果是字符串
                        if(true == $("#show_img_title").attr("checked")){
                                alert("是字符串true");
                        }
                        if("checked" == $("#show_img_title").attr("checked")){
                                alert("是字符串checked!");
                        }
                });
        }
);

//解决不同版jquery的问题,可以用下面的方式判断
function sovle_jquery_virsion(){
        if("checked" == $('#show_img_title', parent.document.body).attr("checked") || true == $('#show_img_title', parent.document.body).attr("checked")){
                alert("多选框被选中!");
        }else{
                alert("多选框没有被选中!");
        }
}

</script>
<title>无标题文档</title>
</head>
<input type="checkbox" id="show_img_title" style="vertical-align:-3px;"><label style="margin: 0 0 0 4px;" for="show_img_title">照片标题描述带入日记</label><br>
<button id="test">test</button><br>
<button id="jianrong">兼容性问题测试</button>
<body>
</body>
</html>

冰释一片天 发表于 2012-03-12 19:59

谢谢分享
页: [1]
查看完整版本: jquery 选中checkbox兼容问题