表单提交 - 防止页面重新加载jQuery
我正在建立一个联系表单,我想通过ajax提交数据,并通过相同的显示结果。但是每当用户点击提交按钮,该页面似乎自动提交。我也使用了jquery的 preventDefault 和 stopPropogation 。但它仍然不工作。我是新的jQuery和我可能会缺少一些小的。以下是代码:
I'm building a contact form and i want to submit the data through ajax and display the result back via the same. But whenever user clicks on submit button, the page seems to be submitting automatically. I have used jquery's preventDefault and stopPropogation too. But its still not working. I'm new to jQuery and I might be missing something small. Here's the code:
表单
<form action="#" method="post" accept-charset="utf-8" name="contactform" id="contactform">
<p>Name<br/><input type="text" name="cname" value="" id="cname" required /></p><br/>
<p>Email<br/><input type="email" name="cemail" value="" id="cemail" required /></p><br/>
<p>Message<br/><textarea name="cmessage" cols="40" rows="10" id="cmessage" required ></textarea></p>
//Re-captcha code here
<p><input type="submit" name="contact_submit" value="Submit" id="contact_submit" /></p>
</form>
<br />
<div id="result"></div>
脚本
$(document).ready( function(){
$("#contact_submit").click( function(e){
e.preventDefault();
e.stopPropagation();
});
$("#result").html('');
$("#contactform").validate();
});
我已经包含jQuery验证插件,也不工作。
I have included jQuery Validation Plugin which also isn't working.
jQuery需要#
来标识id和。
jQuery needs a #
to identify id and .
for class.
如果您不想提交表单,请使用:
If you do not want to submit the form use this:
$("#contactform").on('submit', function(e){
alert("not submited");
return false;
});