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

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

问题描述

我有以下代码:

...<div><a href="#" onclick="SubmitForm();">点击</a>

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

解决方案

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

如果您真的想在 Javascript 中获得响应(不刷新页面),那么您将需要使用 AJAX,而当您开始谈论使用 AJAX 时,您将需要使用图书馆.jQuery 是迄今为止最受欢迎的,也是我个人的最爱.有一个很棒的 jQuery 插件,名为 Form,它可以完全按照您的意愿行事.

以下是您使用 jQuery 和该插件的方式:

$('#myForm').ajaxForm({url : 'myscript.php',//或其他数据类型:'json',成功:功能(响应){alert("服务器说:" + response);}});

I have the following code:

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

            form1.submit();
        }

        function ShowResponse()
        {

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

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?

解决方案

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.

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.

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天全站免登陆