如何使用不同的操作通过AJAX提交表单?

时间:2022-11-23 16:41:14

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).

我有一个表单元素,并且有一个普通的提交按钮,但我也希望能够使用表单中输入的值提交AJAX get请求,但是将其设置为不同的地址(这与提交按钮无关)。

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.

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

1 个解决方案

#1


1  

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,jQuery,Closure或其他几个),这将是最简单的。在这样的库的帮助下,这样做变得非常简单。

Prototype example:

原型示例:

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

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).

使用Prototype的Form#请求,但你可以使用Ajax.Request代替并通过Form#serialize获取表单数据(Form#request基本上就是为你做的)。

jQuery example:

jQuery示例:

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

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

这使用了jQuery的$ .get和serialize函数(你可以使用$ .ajax而不是$ .get来获得更多控件/选项)。

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

没有图书馆这样做是完全可能的,这只是更多的工作。

#1


1  

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,jQuery,Closure或其他几个),这将是最简单的。在这样的库的帮助下,这样做变得非常简单。

Prototype example:

原型示例:

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

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).

使用Prototype的Form#请求,但你可以使用Ajax.Request代替并通过Form#serialize获取表单数据(Form#request基本上就是为你做的)。

jQuery example:

jQuery示例:

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

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

这使用了jQuery的$ .get和serialize函数(你可以使用$ .ajax而不是$ .get来获得更多控件/选项)。

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

没有图书馆这样做是完全可能的,这只是更多的工作。