【问题标题】:How to disable or hide submit button after form validate and submited by ajax?如何在表单验证后禁用或隐藏提交按钮并通过 ajax 提交?
【发布时间】:2019-03-03 16:14:29
【问题描述】:

需要一个 disablehide 在验证后提交按钮的脚本,并 100% 提交 ajax 的表单。

当前代码:

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

但是什么也没发生!在success 发送之后按钮不是disabled。甚至更多...需要submit 按钮在 100% success 发送和通过 php 脚本处理后隐藏(而不是在错误或其他错误之后)...对不起我的英语不好,非常感谢大家。让力量与你同在!

【问题讨论】:

  • 看起来您在启动 ajax 时禁用了该按钮,并在返回时启用它。响应是否被放入元素中?这是一个简单的例子,它发生得如此之快以至于你看不到它?如果需要,您可以使用 Chrome 引入延迟。
  • 任何控制台错误?
  • 您可以使用 onclick() 代替 onsubmit()。我认为您的问题将通过 onclick() 解决
  • @mahdikhodabandello...你的例子不感兴趣,因为它被禁用,即使表单有错误
  • @Twisty...一切正常...没有错误..

标签: javascript jquery ajax forms ecmascript-6


【解决方案1】:

您可能需要考虑以下代码:

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');
}

这会将.hide() 添加到success 中的按钮。

【讨论】:

  • 对不起.. 但什么也没发生
  • BEST...但是按钮隐藏甚至表单错误...而且它害羞((您可以在这里看到它:nirvana.ho.ua/coffee/?vichy=upst#zakaz ...即使表单为空,您也可以单击按钮并且它将隐藏(
  • @ВикторияСикрет 我在控制台中看到 2 个错误:TypeError: document.getElementsByClassName(...)[0] is undefined[了解更多] flyeing.buttons.and.scroll.js:232 :1 nirvana.ho.ua/coffee/js/flyeing.buttons.and.scroll.js:232:1 And: ReferenceError: domReady is not defined[Learn More] preloader.js:4:405 nirvana.ho.ua/coffee/js/preloader.js:4:405 然而这个函数作为预期,Ajax 被触发并且按钮被隐藏,如你所要求的。如果需要执行验证,则需要添加。
  • 添加什么?抱歉我看不到
  • 关于控制台错误...如果您看到向上按钮和左右飞行按钮 - 那么一切都很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多