问题
I have a div
<div id="fade_bg">
<div id="login_div">
<form action="../classes/login/Authenticator.php" method="post">
<p>username: <input type="text" name="username" /></p>
<p>password: <input type="password" name="password" /></p>
<p><input type="submit" name="login" /></p>
</form>
<p><a id="cancel" href="#">cancel</a>
</div>
</div>
That I only want to show when
<a id="login" href="">Admin login</a>
is clicked. I used
$(function() {
$('a#login').click(function() {
$('#fade_bg').show();
$('#login').hide();
});
$('a#cancel').click(function() {
$('#fade_bg').hide();
$('#login').show();
});
});
However, when I click the link, the div I want to appear only appears for a split second, then returns to normal as if I clicked the cancel button. Why is this happening?
Edit: No errors on the JS debugger console.
回答1:
Try like this:
$('#login').click(function (e) {
e.preventDefault();
$('#fade_bg').show();
$(this).hide();
});
$('#cancel').click(function (e) {
e.preventDefault();
$('#fade_bg').hide();
$('#login').show();
});
DEMO HERE
回答2:
The submit button always post the form so
<input type="submit" name="login" />
Replace it with
<input type="button" name="login" />
来源:https://stackoverflow.com/questions/16445408/jquery-show-only-works-for-a-split-second