【问题标题】:Jquery Mobile - events wont bind after using $.mobile.navigateJquery Mobile - 使用 $.mobile.navigate 后事件不会绑定
【发布时间】:2014-04-09 17:19:47
【问题描述】:

当我使用 $.mobile.navigate 更改页面时,页面将加载,但我的自定义脚本不会绑定到元素。如果我刷新页面,自定义脚本将加载并绑定到元素。我有一个选择元素可以在页面之间进行选择:

<select name="calc-Nav" id="calc-Nav">
    <option value="a.php">A</option>
    <option value="b.php">B</option>
    <option value="c.php">C</option>
</select>

这是绑定到选择元素的事件:

$("#calc-Nav").on("change", function (e) {
    var opt = $("#calc-Nav option:selected").val();
    if (opt) {
        e.preventDefault();
        $.mobile.navigate(opt);
    }
});

另外,我按以下顺序链接到我的 javascript 文件:

<script src="jquery.js"></script>
<script src="custom-scripting.js"></script>
<script src="jquery-mobile.js"></script>

有没有人知道如何使这项工作?

谢谢。

编辑: 这是用于所有页面的模板 这是每个页面的标准模板。

<!DOCTYPE html>
<html>
<head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">
</head>
<body>
    <div data-role="page">
        <div data-role="header">
            <div class="ui-field-contain">
                <select name="calc-Nav" id="calc-Nav">
                    <option value="Index.php">Home</option>
                    <option value="a.php">a</option>
                    <option value="b.php">b</option>
                    <option value="c.php">c</option>
                </select>
            </div>
         </div>
     <div data-role="main" class="ui-content">
         <div id="index">
             <h1> Form goes Here. </h1>
         </div>
     </div>
     <div data-role="footer">
         <h1>Footer</h1>
     </div>
 </div>
</body>
    <script src="http://code.jquery.com/jquery-1.9.0.js"></script>
    <script src="js/formulas.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
</html>

【问题讨论】:

    标签: javascript jquery html jquery-mobile mobile


    【解决方案1】:

    您需要将它们绑定到文档

    试试-

    $(document).on("change","#calc-Nav", function (e) {
        var opt = $("#calc-Nav option:selected").val();
        if (opt) {
            e.preventDefault();
            $.mobile.navigate(opt);
        }
    });
    

    还要确保在 jqmobile 脚本之后添加自定义脚本的链接

    【讨论】:

    • 还是没有运气。视图源在刷新后看起来。任何想法
    • @ButtScratch 你的每一页都有这个选择框吗?你能把整个代码页显示一个 js 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 2015-10-28
    • 1970-01-01
    相关资源
    最近更新 更多