火狐 event 遇到问题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="http://code.jquery.com/jquery-2.2.4.min.js"></script>
</head>
<body>
    <input type="radio" id="" onclick="fn()" value="1" name="rd">
    <input type="radio" id="" onclick="fn()" value="2" name="rd">
    <input type="radio" id="" onclick="fn()" value="3" name="rd">
    <input type="radio" id="" onclick="fn()" value="4" name="rd">
    <input type="radio" id="" onclick="fn()" value="5" name="rd">
    <script>
        function fn(){
            var target = window.event.srcElement || window.event.target;
            // if(){}
            console.log($(target).val(),$(target).prop("checked"));
            
        }
    </script>
</body>
</html>
 
这种写法在谷歌中是能正常使用的  但是在火狐中是不能的保错是event 未定义
 
解决方法:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="http://code.jquery.com/jquery-2.2.4.min.js"></script>
</head>
<body>
    <input type="radio" id="" onclick="fn(event)" value="1" name="rd">
    <input type="radio" id="" onclick="fn(event)" value="2" name="rd">
    <input type="radio" id="" onclick="fn(event)" value="3" name="rd">
    <input type="radio" id="" onclick="fn(event)" value="4" name="rd">
    <input type="radio" id="" onclick="fn(event)" value="5" name="rd">
    <script>
        function fn(event){
            var target = event.srcElement || event.target;
            // if(){}
            console.log($(target).val(),$(target).prop("checked"));
            
        }
    </script>
</body>
</html>
或者:因为我是获取radio的值  也可以换中方法
var val=$('input:radio[name="rd"]:checked').val()
根据实际情况来看吧。
posted @ 2019-03-02 10:22  吃饭睡觉,打豆豆  阅读(247)  评论(0编辑  收藏  举报