ie中的radio click()不能触发radio的change事件

来源:互联网 发布:cad网络机柜 交换机 编辑:程序博客网 时间:2024/05/16 06:44

jquery版本:1.7.2
浏览器版本:ie8
测试代码如下,实现的功能是页面加载后,点击单选框的第一项。使之成为选中的状态

<html lang="zh-cn"><head>    <meta charset="utf-8"></head>    <body>    <input type="radio" name="gender" value="female"><input type="radio" name="gender" value="male"><!--1.7.2-->    <script   src="https://code.jquery.com/jquery-1.7.2.min.js"   integrity="sha256-R7aNzoy2gFrVs+pNJ6+SokH04ppcEqJ0yFLkNGoFALQ="   crossorigin="anonymous"></script>    <!--<script   src="https://code.jquery.com/jquery-1.12.4.min.js"   integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="   crossorigin="anonymous"></script>-->        <script>        $("input[name=gender]").on('change',function(){            alert($(this).val());        });        $("input[name=gender]").eq(0).click();         </script>    </body></html>

以上代码在ie8中不生效,对话框不会弹出,改正方法有2种:
1. 改变jquery的版本,使用新版本的jquery就会弹出对话框。这是jquery1.7.2的bug。
2. 将radio的change事件,变成click事件。这个时候就需要判断单选框的状态了,代码变成下面这样:

<script>        $("input[name=gender]").on('click',function(){        if($(this).prop('checked')){            alert($(this).val());            }        });        $("input[name=gender]").eq(0).prop('checked',true).click();    </script>

在项目开发中,因为历史原因,不能升级jquery版本,只能采取第二种方法,

0 0