表单提交 - 防止页面重新加载jQuery [英] Form Submit - Preventing page reload jQuery

查看:68
本文介绍了表单提交 - 防止页面重新加载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;

    });

这篇关于表单提交 - 防止页面重新加载jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆