如何使用不同的操作通过AJAX提交表单? [英] How to submit a form via AJAX using a different action?

查看:137
本文介绍了如何使用不同的操作通过AJAX提交表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单元素,它有一个正常的提交按钮,但我也希望能够使用表单中输入的值提交一个AJAX get请求,但是将其提交到另一个地址(这与提交按钮无关)。

I have a form element and that has a normal submit button but I also want to be able to submit an AJAX get request using the values entered in the form but make it to a different address (this is independed of the submit button).

有没有简单的方法可以做到这一点?

Is there an easy way to do that?

长版本:我有一个包含几个选择元素的表单,并且希望在用户每次设置其中一个时重新填充它们(将它们更新为新的有效值集合)。

The long version: I have a form with several select elements in it and want to repopulate them (updating them to the new set of valid values) each time the user sets one of them.

推荐答案

如果您使用库( Prototype jQuery Closure ,或其他人)。在这样一个图书馆的帮助下,这样做几乎非常容易。

This will be easiest if you use a library (Prototype, jQuery, Closure, or any of several others). With the help of such a library, it becomes almost trivially easy to do this.

Prototype example:

$('theFormElementId').request({
    onSuccess: function(response) {
        // GET was successful
    }
});

使用Prototype的 Form#request ,但您可以使用 Ajax.Request ,并通过 Form#serialize Form#请求基本上就是为你做的)

that uses Prototype's Form#request, but you could use Ajax.Request instead and get the form data via Form#serialize (Form#request basically just does that for you).

jQuery example:

$.get("yoururl", $('#theFormElementId').serialize(), function(data) {
    // The GET was successful
});

使用jQuery的 $。get $。ajax 而不是 $。get 获取更多控制/选项)。

That uses jQuery's $.get and serialize functions (you could use $.ajax instead of $.get for more control/options).

完全可以在没有图书馆的情况下做到这一点,这只是更多的工作。

It's totally possible to do this without a library, it's just a lot more work.

这篇关于如何使用不同的操作通过AJAX提交表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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