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

javascript - Use jQuery to ignore form submit when clicking an input - Stack Overflow

programmeradmin3浏览0评论

I have a form with a submit input and I need that when the button gets clicked, my script do some Ajax actions. Also I need to disable that button during the Ajax request. So I need a jQuery mand to avoid real form submit when input is clicked. I tried to use this:

$('input.searchplease').click(function () {
    $(this).attr('disabled', 'disabled');

    alert('Yep');
    //Do Ajax
});

This didn't worked. I mean, the alert shown correctly but form is submitted. So what is your suggestion?

I have a form with a submit input and I need that when the button gets clicked, my script do some Ajax actions. Also I need to disable that button during the Ajax request. So I need a jQuery mand to avoid real form submit when input is clicked. I tried to use this:

$('input.searchplease').click(function () {
    $(this).attr('disabled', 'disabled');

    alert('Yep');
    //Do Ajax
});

This didn't worked. I mean, the alert shown correctly but form is submitted. So what is your suggestion?

Share Improve this question edited Apr 17, 2012 at 12:47 Juan G. Hurtado 2,14716 silver badges25 bronze badges asked Apr 17, 2012 at 12:07 SaeidSaeid 13.6k34 gold badges109 silver badges178 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 7

Try this:

$('input.searchplease').click(function (e) {
   $(this).attr('disabled', 'disabled');

   alert('Yep');
   //Do Ajax

   e.preventDefault();
});
$('input.searchplease').click(function () {
    alert('yep');
    return false;
});

This should work

You can do this:

$('form').submit(function(){
    ... ajax things

    return false;
});

I would rather not disable the button (and then enable it), unless I have to.

try this way :

$("#id-of-your-form").submit(function() {

    $("#id-of-your-submit-input").attr("disabled", true);

    // do-ajax
    // you can use jquery's ajax plete handler to enable the submit button again

    return false;
});

I think this will do the trick:

$('form').submit(function(event) {
    event.preventDefault();
    $('#submit-button-ID').attr('disabled', 'disabled');
    $.ajax({
        ...,
        success: function() {
                $('#submit-button-ID').removeAttr('disabled');
              }
    });
});
发布评论

评论列表(0)

  1. 暂无评论