admin管理员组文章数量:1391995
This is my HTML code :
<form id="change-ums" class="form-horizontal" style="margin-top: 10px;">
<div class="control-group">
<label class="control-label" for="umsid">ID :</label>
<div class="controls"><input type="text" id="umsid" required/></div>
</div>
<div class="control-group">
<label class="control-label" for="umspass">Password :</label>
<div class="controls">
<input type="password" id="umspass" required/>
<br>
<br>
<button type="submit" class="btn btn-primary btn-small">Change Pass</button>
</div>
</div>
</form>
And this is my Jquery Code :
$('change-ums').on('submit',function(e){
e.preventDefault();
$.ajax({
type : 'post',
url : '/user/changepass',
data : $('change-ums').serialize(),
success : function(){
alert('Your password has been changed successfully.');
}
});
});
I think that I am doing everything correctly, and even using preventDefault()
but still the page is getting refreshed and the jquery is not able to plete its request. What am I doing wrong?
This is my HTML code :
<form id="change-ums" class="form-horizontal" style="margin-top: 10px;">
<div class="control-group">
<label class="control-label" for="umsid">ID :</label>
<div class="controls"><input type="text" id="umsid" required/></div>
</div>
<div class="control-group">
<label class="control-label" for="umspass">Password :</label>
<div class="controls">
<input type="password" id="umspass" required/>
<br>
<br>
<button type="submit" class="btn btn-primary btn-small">Change Pass</button>
</div>
</div>
</form>
And this is my Jquery Code :
$('change-ums').on('submit',function(e){
e.preventDefault();
$.ajax({
type : 'post',
url : '/user/changepass',
data : $('change-ums').serialize(),
success : function(){
alert('Your password has been changed successfully.');
}
});
});
I think that I am doing everything correctly, and even using preventDefault()
but still the page is getting refreshed and the jquery is not able to plete its request. What am I doing wrong?
2 Answers
Reset to default 8you missed #
in your selector.
$('change-ums').on('submit',function(e){
should be
$('#change-ums').on('submit',function(e){
^
Your selector should be $('#change-ums')
(# = id)
Also, be sure that your script is loaded after the dom loading.
本文标签: javascriptPrevent page getting refreshed on form submit via JQueryStack Overflow
版权声明:本文标题:javascript - Prevent page getting refreshed on form submit via JQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744647729a2617499.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论