最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Prevent page getting refreshed on form submit via JQuery - Stack Overflow

programmeradmin1浏览0评论

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?

Share Improve this question asked Sep 27, 2013 at 12:07 Sankalp SinghaSankalp Singha 4,5466 gold badges40 silver badges60 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

you 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.

发布评论

评论列表(0)

  1. 暂无评论