jQuery (mobile) not triggering .change event on Phonegap app

℡╲_俬逩灬. 提交于 2020-01-04 04:20:09

问题


(because 2 days I had no issues with this...)

I have a multipage layout, in one of my pages I have a structure like so:

<div id="view_offer" data-role="page">
        <div data-role="header">
            <h1>Edit Offer</h1>
        </div>
        <div data-role="content">
            <p><label><input id="auto_renew" type="checkbox" name="checkbox-0">Auto renew?</label></p>
        </div>
    </div>

With the following scripts (in this order) before I close the body tag:

    <script src="js/cordova-2.5.0.js"></script>
    <script src="js/jquery-1.8.2.min.js"></script>
    <script src="js/jquery.mobile-1.3.0.min.js"></script>
    <script src="js/home_m_scripts.js"></script>

My home_m_scripts.js file:

$("#auto_renew").change(function(){
    alert("Changed!");
});

My change event is not triggering though, I'm at loss here.


回答1:


Change your code from:

$("#auto_renew").change(function(){
    alert("Changed!");
});

To:

$(document).on('change','#auto_renew',function(){ 
    alert("Changed!"); 
});

When you bind an event like this it will act as an event delegation. Basically object don't need to exist in a DOM when you execute this binding.



来源:https://stackoverflow.com/questions/15948353/jquery-mobile-not-triggering-change-event-on-phonegap-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!