表格发送成功后如何隐藏“提交”按钮?

时间:2013-09-28 14:44:12

标签: javascript jquery forms

我希望在成功发送表单时隐藏表单的“提交”按钮。我试图在$('#submit').hide(2000);之后添加$('.success').fadeIn(1000);  但没有成功。 感谢

HTML:

<div class="block-right">  <h1>Formulaire de contact</h1>
            <!-- CONTACT FORM-->
            <div class="contact-form">


                <form id="contactfrm" method="post" class="clearfix">
                    <div class="clearfix">
                        <input id="name" name="name" placeholder="Name" type="text" value="">
                        <input id="email" name="email" placeholder="Email" type="email" value="">
                    </div>

                    <textarea id="message" name="message" placeholder="Message"></textarea>

                    <input type="submit" value="Envoyer" name="submit">

  <p class="success" style="display:none">Your message has been sent successfully.</p>
  <p class="error" style="display:none">E-mail must be valid and message must be longer than 100 characters.</p>
                </form>
            </div><!-- /.contact-form -->
</div>  <!-- End DIV block-right -->

JS:

// Contact Form
$(document).ready(function(){
  $("#contactfrm").submit(function(e){
     e.preventDefault();
  var name = $("#name").val();
  var email = $("#email").val();
  var message = $("#message").val();
  var dataString = 'name=' + name + '&email=' + email + '&message=' + message;
  function isValidEmail(emailAddress) {
    var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
    return pattern.test(emailAddress);
  };

  if (isValidEmail(email) && (message.length > 10) && (name.length > 1)){
    $.ajax({
    type: "POST",
    url: "sendmessage.php",
    data: dataString,
    success: function(){
      $('.success').fadeIn(1000);
    }
    });
  } else{
    $('.error').fadeIn(1000);
  }

  return false;
});
});

3 个答案:

答案 0 :(得分:1)

当触发ajax成功功能时,隐藏按钮。

$this.find('input:submit').hide();

您的代码变为

$(document).ready(function () {
    $("#contactfrm").submit(function (e) {
        var $this =$(this);//added this code here
        e.preventDefault();
        var name = $("#name").val();
        var email = $("#email").val();
        var message = $("#message").val();
        var dataString = 'name=' + name + '&email=' + email + '&message=' + message;

        function isValidEmail(emailAddress) {
            var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
            return pattern.test(emailAddress);
        };

        if (isValidEmail(email) && (message.length > 10) && (name.length > 1)) {
            $.ajax({
                type: "POST",
                url: "sendmessage.php",
                data: dataString,
                success: function () {
                    $('.success').fadeIn(1000);
                    $this.find('input:submit').hide();//added this code here
                }
            });
        } else {
            $('.error').fadeIn(1000);
        }

        return false;
    });
});

参考

http://api.jquery.com/hide/

http://api.jquery.com/find/

答案 1 :(得分:1)

更改

  <input type="submit" value="Envoyer" name="submit">

    <input type="submit" value="Envoyer"  id="submit" name="submit">

答案 2 :(得分:0)

试试这个:

success: function(){
      $('.success').fadeIn(1000);
      $('input[name="submit"]').hide();
    }

但我建议你将提交的html更改为:

<input type="submit" value="Envoyer" name="submit" id="submit">

并使用此代码,这样您就可以只隐藏表单的提交:

success: function(){
          $('.success').fadeIn(1000);
          $('#submit').hide();
        }