jQuery on second click doesn't work - mobile

房东的猫 提交于 2019-12-13 10:05:19

问题


Before you say that is duplicate from other topics, I've seen that topics, such as:

jQuery: trigger click() doesn't work?

jQuery .on Click second time not working

Jquery Onclick not happening second time

one Jquery is not working on the second click

And none of that help me. My code is very simple:

index.php

/* in body tag */

<input type="button" id="page-add" value="Add" data-role="none"/>

/* after body tag */
$(function(){
    $("#page-add").click(function(e){
        window.location.href = 'add.php';   
    });
});

add.php

/* in body tag */
<div class="ui-btn-left"><a href="index.php"><img src="images/logo.png"/></a></div>

The button "page-add" onClick event works for the first time, when I click the <a href> and it backs to the index.php, if I click again on the same button it doesn't work no more. I've to refresh the page so it can work again.

I've tried to add before the window.location.href the code e.preventDefault() but didn't work either. I don't receive any errors on console.

What am I doing wrong?

Edit: Solved.

$(document).on('pagebeforeshow', '#main', function(){     
    $('#page-add').bind('click',function(){
        $.mobile.changePage('add.php');
    });
});

回答1:


Try jQuery on function for the same,

$( "element" ).on( "click", function() {
alert("clicked");
});



回答2:


Call Function using Onclick event for Button Try Something like

index.php

/* in body tag */

<input type="button" id="page-add" onclick='addFunction();' value="Add" data-role="none"/>

/* after body tag */
function addFunction() {
        window.location.href = 'add.php';   
}

add.php

/* in body tag */
<div class="ui-btn-left"><a href="index.php"><img src="images/logo.png"/></a></div>


来源:https://stackoverflow.com/questions/20784157/jquery-on-second-click-doesnt-work-mobile

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