防止表单重定向或提交刷新? [英] Prevent form redirect OR refresh on submit?

查看:33
本文介绍了防止表单重定向或提交刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了一堆页面,但找不到我的问题,所以我不得不发帖.

I've searched through a bunch of pages, but can't find my problem, so I had to make a post.

我有一个带有提交按钮的表单,提交时我希望它不刷新或重定向.我只是想让 jQuery 执行一个功能.

I have a form that has a submit button, and when submitted I want it to NOT refresh OR redirect. I just want jQuery to perform a function.

表格如下:

<form id="contactForm">
    <fieldset>
        <label for="Name">Name</label>
        <input id="contactName" type="text" />
    </fieldset>

    <fieldset>
        <label for="Email">Email</label>
        <input id="contactEmail" type="text" />
    </fieldset>

    <fieldset class="noHeight">
        <textarea id="contactMessage" cols="20"></textarea>
        <input id="contactSend" class="submit" type="submit" onclick="sendContactForm()" />
    </fieldset>
</form>        
<small id="messageSent">Your message has been sent.</small>

这里是 jQuery:

And here is the jQuery:

function sendContactForm(){
    $("#messageSent").slideDown("slow");
    setTimeout('$("#messageSent").slideUp();$("#contactForm").slideUp("slow")', 2000);
}

我试过在表单上有和没有动作元素,但不知道我做错了什么.更让我恼火的是,我有一个完美的例子:示例页面

I've tried with and without an action element on the form, but don't know what I'm doing wrong. What has annoyed me more is that I have an example that does it perfectly: Example Page

如果您想实时查看我的问题,请转到 stormink.net(我的网站)并查看侧边栏说给我发电子邮件"和RSS订阅".两者都是我试图让它发挥作用的形式.

If you want to see my problem live, goto stormink.net (my site) and check out the sidebar where it says "Send me and email" and "RSS Subscription". Both are forms that I'm trying to get this to work on.

推荐答案

只处理submit上的表单提交 事件,并返回 false:

Just handle the form submission on the submit event, and return false:

$('#contactForm').submit(function () {
 sendContactForm();
 return false;
});

您不再需要提交按钮上的 onclick 事件:

You don't need any more the onclick event on the submit button:

<input class="submit" type="submit" value="Send" />

这篇关于防止表单重定向或提交刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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