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

javascript - form.submit() not including sending submit button - Stack Overflow

programmeradmin0浏览0评论

i have a code in jQuery which first validate the form using ajax and then resubmit the same form to another location but while resubmitting the submit button value not sending.. What will be the issue??

here's the js code:

$.ajax({
    'type': 'post',
    'url': '/inc/do.php',
    'data': $form.serialize(),
    beforeSend: function(){
        $form.find(':input').attr('disabled', true);
        $form.find('.btn').addClass('bdsbld');
        $form.find('.blkr').css('display', 'block');
    },
    error: function(){
        Recaptcha.reload();
        $form.find(':input').attr('disabled', false);
        $form.find('.btn').removeClass('bdsbld');
        $form.find('.blkr').css('display', 'none');
    },
    success: function(d){
        $form.find(':input').removeAttr('disabled');
        if(res=isJSON(d)){
            if(!res.err && res.msg.toLowerCase()==='ok'){
                if(($PRI_KEY=$form.find('input#PRIVATE_KEY')).length) $PRI_KEY.val(res.PRIVATE_KEY);
                $form.append('<input type="hidden" id="PRIVATE_KEY" name="PRIVATE_KEY" value="'+res.PRIVATE_KEY+'" />');
                $form.find('input#do').val('BOOK_AD');
                $form.find('#reCaptchaO').remove();
                form.submit();
            }
            else{
                alert(res.msg);
                Recaptcha.reload();
                $form.find('.btn').removeClass('bdsbld');
                $('#'+res.fld).delay(250).focus();
            }
        }
        else
        if(d!==''){
            alert(d);
            Recaptcha.reload();
            $form.find('.btn').addClass('bdsbld');
        }
        $form.find('.blkr').css('display', 'none');
    }
});

in this code, the line i.e.

form.submit();

is re-submitting the form but it is not including the submit button and its value but the submit button was included in the ajax request... why??

i have a code in jQuery which first validate the form using ajax and then resubmit the same form to another location but while resubmitting the submit button value not sending.. What will be the issue??

here's the js code:

$.ajax({
    'type': 'post',
    'url': '/inc/do.php',
    'data': $form.serialize(),
    beforeSend: function(){
        $form.find(':input').attr('disabled', true);
        $form.find('.btn').addClass('bdsbld');
        $form.find('.blkr').css('display', 'block');
    },
    error: function(){
        Recaptcha.reload();
        $form.find(':input').attr('disabled', false);
        $form.find('.btn').removeClass('bdsbld');
        $form.find('.blkr').css('display', 'none');
    },
    success: function(d){
        $form.find(':input').removeAttr('disabled');
        if(res=isJSON(d)){
            if(!res.err && res.msg.toLowerCase()==='ok'){
                if(($PRI_KEY=$form.find('input#PRIVATE_KEY')).length) $PRI_KEY.val(res.PRIVATE_KEY);
                $form.append('<input type="hidden" id="PRIVATE_KEY" name="PRIVATE_KEY" value="'+res.PRIVATE_KEY+'" />');
                $form.find('input#do').val('BOOK_AD');
                $form.find('#reCaptchaO').remove();
                form.submit();
            }
            else{
                alert(res.msg);
                Recaptcha.reload();
                $form.find('.btn').removeClass('bdsbld');
                $('#'+res.fld).delay(250).focus();
            }
        }
        else
        if(d!==''){
            alert(d);
            Recaptcha.reload();
            $form.find('.btn').addClass('bdsbld');
        }
        $form.find('.blkr').css('display', 'none');
    }
});

in this code, the line i.e.

form.submit();

is re-submitting the form but it is not including the submit button and its value but the submit button was included in the ajax request... why??

Share Improve this question asked Sep 12, 2011 at 20:48 Vaibhav GuptaVaibhav Gupta 1,6121 gold badge14 silver badges23 bronze badges 3
  • any workaround for this?? I want the form to be resubmitted with all fields & buttons.. – Vaibhav Gupta Commented Sep 12, 2011 at 20:59
  • it seems entirely unnecessary for you to require a static value field contained in a submit button. See my answer for possible alternative. – jondavidjohn Commented Sep 12, 2011 at 21:02
  • hmmm... its seems you are correct to add dynamic value... thx – Vaibhav Gupta Commented Sep 12, 2011 at 21:04
Add a ment  | 

3 Answers 3

Reset to default 7

Submit buttons are only considered successful controls if they are used to submit the form. Since you are submitting the form using JavaScript, you aren't using the submit button, so it isn't successful, and it isn't included in submitted request.

This is because the submit button is only included in the form submission traditionally when said submit button is clicked.

since you're not submitting the form via the traditional input="submit" button, no value is included.

If you're checking for this value on the server-side, it's a static value that could be easily acplished with a static hidden input, doesn't seem to be a reason to fake a clicked submit button to retrieve a static value.

<input type="hidden" value="info" name="field_name" id="field_name" />

You can use this method before form.submit(). call the submit button with click function. so, it traditionally calls the submit button.

document.form name.submit button name.click();

发布评论

评论列表(0)

  1. 暂无评论