Javascript 我想在不刷新页面的情况下从提交联系人

Javascript 我想在不刷新页面的情况下从提交联系人,javascript,jquery,html,css,forms,Javascript,Jquery,Html,Css,Forms,我在我的网站上有一个发送电子邮件的联系方式。我希望它不刷新页面时提交,但发送电子邮件和成功消息。我的表格是 <form method="post" id="contactform" enctype="multipart/form-data"> <input class="contactusfeild" style="width:222px;" name="sendername" type="text" placeholder="Your Name" required>

我在我的网站上有一个发送电子邮件的联系方式。我希望它不刷新页面时提交,但发送电子邮件和成功消息。我的表格是

<form method="post" id="contactform" enctype="multipart/form-data">
<input class="contactusfeild" style="width:222px;" name="sendername"  type="text" placeholder="Your Name" required>
<input class="contactusfeild" name="senderemail" style="width:222px;"  type="email" placeholder="Your Email" required >
<textarea name="sendermessage" class="contactusfeild" type="text" placeholder="Your Message Here" style="width:220px; max-width:220px; height:100px; max-height:100px; padding-top:10px;" required ></textarea>
<input  class="button"  style="border:none;" type="reset"  value="Clear" /><input  name="contactcozmuler" class="button" style="border:none; margin " type="submit"  id="submit_btn" value="Send" />
</form>

使用ajax post。请参阅an和jQuery文档: ,

首先要做的事

您不能使用ajax上传文件,但如果您将使用第三方脚本进行上传

您可以使用jQuery轻松提交表单而无需刷新页面

我写这个例子是为了明确谁在搜索这个问题

如果希望所有表单都使用ajax进行提交,那么只能将“表单”标记定义为jquery选择器

<script type="text/javascript">

//Call Document load
$(function(){

    // Find form elements to use ajax with
    $targetForms = $('form.ajax'); // In here, we are selecting forms that only has ajax class

    // By using submit function, you can use html5 required validation.
    // If you want to on click with buttons, html5 validation will not work.
    $targetForms.submit(function(){
        var $this = $(this); // This is now your form element.

        $.ajax({
            url: $this.attr("action"), //Where do you want to make request?
            type: $this.attr("method"), //Which method will use for request POST or GET?
            dataType: "html",
            data: $this.serialize(), // Collect all form data
            beforeSend: function(){
                // You can define what will happen just when before request.
            },
            success: function(response){,
                /* Lets think here what we can return and which type data will be. 
                   For example if you will return a string and then dataType must be choose HTML. 
                   If message successfully sent to email or recorded to db, you can send here a string like "sent". 
                   Or if error occures, you can send here a string like "error". We will use this strings in this example.
                */

                if(response == "sent") {
                    // Animate your form to height:0 and show a div that have message for visitor.

                } else if(response == "error")) {
                    alert("Hey! Something bad happened!");
                }
            },
            error: function(){

            }
        });

        return false;
    });
});
</script>

//调用文档加载
$(函数(){
//查找要使用ajax的表单元素
$targetForms=$('form.ajax');//在这里,我们选择的表单只有ajax类
//通过使用提交功能,您可以使用html5所需的验证。
//如果你想点击按钮,html5验证将无法工作。
$targetForms.submit(函数(){
var$this=$(this);//这现在是您的表单元素。
$.ajax({
url:$this.attr(“操作”),//您想在哪里发出请求?
type:$this.attr(“method”),//哪个方法将用于请求POST或GET?
数据类型:“html”,
data:$this.serialize(),//收集所有表单数据
beforeSend:function(){
//您可以在请求之前定义将发生什么。
},
成功:功能(响应){,
/*让我们在这里思考我们可以返回什么以及数据将是什么类型。
例如,如果您将返回一个字符串,那么数据类型必须为choose HTML。
如果消息成功发送到电子邮件或记录到数据库,您可以在此处发送类似“sent”的字符串。
或者,如果发生错误,您可以向此处发送类似“error”的字符串。在本例中,我们将使用此字符串。
*/
如果(响应=“已发送”){
//将窗体动画设置为高度:0,并显示一个包含访客消息的div。
}否则如果(响应=“错误”)){
警惕(“嘿!发生了不好的事情!”);
}
},
错误:函数(){
}
});
返回false;
});
});

您还没有正确搜索。无论如何,试试这个可能的副本,你将不得不使用AJAX。具体来说,您需要提出POST请求。看看这个答案: