繁体   English   中英

ajax提交后禁用表单输入

[英]Disable form inputs after ajax submission

我有一个通过Ajax提交的表单。 用户发送表单后,文本更改显示表单已成功发送,然后显示填写的表单。 我想显示表单,但我不希望他们重新提交表单,所以我想禁用输入和提交按钮。 我尝试在ajax脚本中添加: $('#submit_btn').className +=" disabled"但它只是刷新页面而不提交任何内容。

ajax脚本如下:

    $(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $(".button").click(function() {
        // validate and process form
        // first hide any error messages
    $('.error').hide();

        var name = $("input#name").val();
        var email = $("inputemail").val();
        var phone = $("inputphone").val();
        var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone;
        //alert (dataString);return false;

        $.ajax({
      type: "POST",
      url: "http://www.green-panda.com/website/panda/webParts/contact-form.php",
      data: dataString,
      success: function() {
        $('#myModalLabel').html("<h3 class='text-success' id='myModalLabel'>Contact Form Submitted!</h3>")
        $('#myModalSmall').html("<p class='muted'>Your submiessions are below. We will be contacting you soon, you may now close this window.</p>")
        $('#submit_btn').className +=" disabled"
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("<i class='icon-ok icon-white'></i>");
        });
      }
     });
    return false;
    });
});
runOnLoad(function(){
  $("input#name").select().focus();
});

在成功提交表单后,我怎么可能禁用输入和按钮?

http://jsfiddle.net/gY9xS/

实际上它比你想做的要简单得多,你不需要禁用输入,只需在ajax请求后取消提交:

$('form').submit(function(){
    return false;
});

将它放在ajax请求的成功处理程序中。

如果要禁用提交按钮,请替换以下错误:

$('#submit_btn').className +=" disabled"

附:

$('#submit_btn').prop("disabled", true);

在我的应用程序中,我只是禁用了提交按钮并显示了一些进度消息

    function submitForm(formObj) {

        // Validate form
        // if (formObj.email.value === '') {
        //     alert('Please enter a email');
        //     return false;
        // }

        formObj.submit.disabled = true;
        formObj.submit.value = 'Log In...';
        return true;
    }


     <form class="form-login" accept-charset="UTF-8" 
      method="POST" action="/login"   onsubmit="return submitForm(this);">
      ......
      <input type="submit" id="login-submit" name="submit" value="Log In">
      </form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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