简体   繁体   中英

How to disable or hide submit button after form validate and submited by ajax?

Need a script that disable or hide submit button after validating and 100% form submitted by ajax .

Current code:

<script>
    function AjaxFormRequest(result_id,formUkrposhta,url) {
        jQuery.ajax({
            url:url,
            type:"POST",
            dataType:"html",
            data:jQuery("#"+formUkrposhta).serialize(),
            beforeSend: function() {
                $("#messegeUkrResult").html('<span class="wbsn-padding-0">Секунду пожалуйста ...</span>');
                $("#send").prop('disabled', true); // disable button
            },
            success:function(response) {
                $("#send").prop('disabled', false); // enable button
                document.getElementById(result_id).innerHTML = response;

            },
            error: function(response) {
                document.getElementById(result_id).innerHTML = '<p class="wbsn-font-futuranew wbsn-font-20 wbsn-text-deep-orange" style="text-sahdow:0 2px 1px #fff;">Возникла ошибка при заказе. Попробуйте еще раз.</p>';
            }
         });

         $(':input', '#formUkrposhta')
            .not(':button, :submit, :reset, :hidden')
            .val('')
            .removeAttr('checked')
            .removeAttr('selected');
    }
</script>

But nothing happened! And button not disabled after success send. Even more... need submit button hiding after 100% success sending and after treatmenting by php script (not after error or another)... Sorry for my bad English and many thanks to all. Let the power be with you!

You may want to consider this code:

function AjaxFormRequest(result_id,formUkrposhta,url) {
  console.log("Ajax Form Request Triggered.");
  jQuery.ajax({
    url: url,
    type: "POST",
    dataType: "html",
    data: jQuery("#" + formUkrposhta).serialize(),
    beforeSend: function() {
      console.log("Before Send Triggered.");
      $("#messegeUkrResult").html('<span class="wbsn-padding-0">Секунду пожалуйста ...</span>');
      $("#send").prop('disabled', true); // disable button
    },
    success:function(response) {
      console.log("Success Triggered:", response);
      $("#send").prop('disabled', false).hide(); // enable button & Hide it
      $("#" + result_id).html(response);
    },
    error: function(response) {
      console.log("Error Triggered:", response);
      $("#" + result_id).html('<p class="wbsn-font-futuranew wbsn-font-20 wbsn-text-deep-orange" style="text-sahdow:0 2px 1px #fff;">Возникла ошибка при заказе. Попробуйте еще раз.</p>');
    }
  });
  $(':input', '#formUkrposhta')
    .not(':button, :submit, :reset, :hidden')
    .val('')
    .removeAttr('checked')
    .removeAttr('selected');
}

This adds .hide() to the button in success .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM