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

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版本,只能采取第二种方法,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值