如何捕获form.submit的响应 [英] How do I capture response of form.submit

查看:177
本文介绍了如何捕获form.submit的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

I have the following code:

<script type="text/javascript">
        function SubmitForm()
        {

            form1.submit();
        }

        function ShowResponse()
        {

        }
</script>
.
.
.
<div>
    <a href="#" onclick="SubmitForm();">Click</a>
</div>

我想要捕获 form1.submit ?我该怎么做呢?我可以在form1.submit方法中注册任何回调函数吗?

I want to capture the html response of form1.submit? How do I do this? Can I register any callback function to form1.submit method?

推荐答案

您无法使用普通的javascript轻松完成此操作。发布表单时,表单输入会发送到服务器,并刷新页面 - 数据在服务器端进行处理。也就是说, submit()函数实际上并不返回任何内容,它只是将表单数据发送到服务器。

You won't be able to do this easily with plain javascript. When you post a form, the form inputs are sent to the server and your page is refreshed - the data is handled on the server side. That is, the submit() function doesn't actually return anything, it just sends the form data to the server.

如果你真的想在Javascript中获得响应(没有页面刷新),那么你需要使用AJAX,当你开始讨论使用AJAX时,你需要来使用图书馆。 jQuery 是迄今为止最流行的,也是我个人最喜欢的。 jQuery有一个很棒的插件叫表单,它可以完成你想要的东西。

If you really wanted to get the response in Javascript (without the page refreshing), then you'll need to use AJAX, and when you start talking about using AJAX, you'll need to use a library. jQuery is by far the most popular, and my personal favourite. There's a great plugin for jQuery called Form which will do exactly what it sounds like you want.

以下是您如何使用jQuery和该插件:

Here's how you'd use jQuery and that plugin:

$('#myForm')
    .ajaxForm({
        url : 'myscript.php', // or whatever
        dataType : 'json',
        success : function (response) {
            alert("The server says: " + response);
        }
    })
;

这篇关于如何捕获form.submit的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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