使用jquery mobile和cordova时将事件绑定到控件 [英] Binding events to controls when using jquery mobile and cordova

查看:86
本文介绍了使用jquery mobile和cordova时将事件绑定到控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在提交表单后将onclick事件重新绑定到按钮。

但是我似乎无法在初始点击后重新绑定点击事件。我在下面创建了一个简短的例子,以突出我正在谈论的内容。



我尝试了什么:



我的基本索引页面如下所示



I am trying to rebind onclick events to buttons after the form has been submitted.
However i can't seem to rebind the click event after the initial click. I've created a brief example below to show highlight what i'm talking about.

What I have tried:

I have a basic index page that looks like the following

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com http://api.openweathermap.org 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
    <title>Test App</title>
    
    
</head>
<body>
    <!-- Cordova reference, this is added to your app when it's built. -->
    <script src="cordova.js"></script>
    <script src="scripts/platformOverrides.js"></script>
    <script src="scripts/jquery-2.2.3.min.js"></script>
    <script src="scripts/jquery.mobile-1.4.5.min.js"></script>
    <script src="scripts/index.js"></script>

    <div id="test-page">
        <div class="ui-content">
            <form>
                <button id="test1" data-role="button">Test 1</button>
            </form>
            <form>
                <button id="test2" data-role="button">Test 2</button>
            </form>
        </div>
    </div>
</body>
</html>





我有一个基本的index.js文件看起来ike以下





I have a basic index.js file that looks like the following

(function () {
    "use strict";

    document.addEventListener("deviceready", onDeviceReady.bind(this), false);

    function onDeviceReady() {
        document.addEventListener('pause', onPause.bind(this), false);
        document.addEventListener('resume', onResume.bind(this), false);
        bindEvents();
    };

    function onPause() {
    };

    function onResume() {
    };

})();

$(document).on('pageload', function () {
    bindEvents();
});

$(document).ready(function () {
    bindEvents();
});

$(document).on('pagechange', function (e, data) {
    bindEvents();
});

function bindEvents() {
    $("#test1").click(function () { alert("test1"); });
    $("#test2").click(function () { alert("test2"); });
}





警告显示第一次点击按钮。然而,随后的点击没有显示任何警报(我已经离开了很多bindEvents调用以显示我尝试过的东西)。

我尝试了更多场景但我无法获得在初始点击完成后绑定的click事件....我做错了什么?



Alerts show the first time a button is clicked. However subsequent clicks aren't showing any alerts (I've left in a lot of bindEvents calls to show what i've tried).
I've tried more scenarios but i just can't get the click event to bind after the initial click is done....what am i doing wrong?

推荐答案

document )。on(' pageload' function (){
bindEvents();
});
(document).on('pageload', function () { bindEvents(); });


document ).ready( function (){
bindEvents();
});
(document).ready(function () { bindEvents(); });


(< span class =code-sdkkeyword> document )。on(' pagechange' function (e,data){
bindEvents();
});

function bindEvents(){
(document).on('pagechange', function (e, data) { bindEvents(); }); function bindEvents() {


这篇关于使用jquery mobile和cordova时将事件绑定到控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆