问题
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