Jquery mobile 复选框onclick和onchange事件调用两次,请参见简单示例

Jquery mobile 复选框onclick和onchange事件调用两次,请参见简单示例,jquery-mobile,Jquery Mobile,如果我注释掉jquery.mobile…js文件,它可以正常工作,包括它,每次单击复选框,onclick事件都会触发两次。onchange事件也是如此。有什么想法吗 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobil

如果我注释掉jquery.mobile…js文件,它可以正常工作,包括它,每次单击复选框,onclick事件都会触发两次。onchange事件也是如此。有什么想法吗

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
</head>
<body>
    <script>
        function tester() { alert("I get called twice on click"); }
    </script>
    <input id="abc" name="myCheckbox" onchange="tester()" type="checkbox" value="true" />

    <label for="myCheckbox">Click me</label>
</body>
</html>

function tester(){alert(“我在单击时被调用两次”);}
点击我

您的标记格式不正确。
标记指向一个不存在的id。它指向“myCheckbox”,但输入标记的id是“abc”。此更改修复了您的问题,并正确增强了标记

<input id="abc" name="myCheckbox" onchange="tester()"
    type="checkbox" value="true" />
<label for="abc">Click me</label>

点击我

您的标记格式不正确。
标记指向一个不存在的id。它指向“myCheckbox”,但输入标记的id是“abc”。此更改修复了您的问题,并正确增强了标记

<input id="abc" name="myCheckbox" onchange="tester()"
    type="checkbox" value="true" />
<label for="abc">Click me</label>

点击我

这是出于设计,因为这两个事件都在发生。见示例:

HTML:

<html>
    <head>
        <title>Checkbox Test</title>
    </head>
    <body>
        <div data-role="page">
            <div data-role="header">
                <h2>Checkbox Test</h2>
            </div>
            <div data-role="content">
                    <input type="checkbox" id="checkbox-choice-1" />
                    <label for="checkbox-choice-1">Click me</label>
            </div>
        </div>
    </body>
</html>
$(document).ready(function(){
    $("#checkbox-choice-1").click(function(){
        alert("Click event triggerd.");
    });
    $("#checkbox-choice-1").change(function(){
        alert("Change event triggerd.");
    });
});

如果可能的话,我建议将脚本代码分开。您可以为一个或两个事件创建一个函数。

这是经过设计的,因为两个事件都在发生。见示例:

HTML:

<html>
    <head>
        <title>Checkbox Test</title>
    </head>
    <body>
        <div data-role="page">
            <div data-role="header">
                <h2>Checkbox Test</h2>
            </div>
            <div data-role="content">
                    <input type="checkbox" id="checkbox-choice-1" />
                    <label for="checkbox-choice-1">Click me</label>
            </div>
        </div>
    </body>
</html>
$(document).ready(function(){
    $("#checkbox-choice-1").click(function(){
        alert("Click event triggerd.");
    });
    $("#checkbox-choice-1").change(function(){
        alert("Change event triggerd.");
    });
});

如果可能的话,我建议将脚本代码分开。您可以为一个或两个事件创建一个函数。

谢谢!我花了4个小时在谷歌上搜索和尝试不同的东西。谢谢!我花了4个小时在谷歌上搜索和尝试不同的东西。