通过jQuery发布数组 [英] Post array via jQuery

查看:56
本文介绍了通过jQuery发布数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,其中包含一些唯一的输入字段和一些其他具有重复名称的字段,例如:

I have a form which contains some unique input fields and some others with duplicate names, like this:

<form method="post">
  Title: <input type="text" name="title" /><br />
  Content: <input type="text" name="content" /><br />
  Name: <input type="text" name="name" /><br />
  Email: <input type="text" name="email" /><br />
  Name: <input type="text" name="name" /><br />
  Email: <input type="text" name="email" /><br />
  Name: <input type="text" name="name" /><br />
  Email: <input type="text" name="email" />
</form>

因此,标题"和内容"只是字符串值,但是名称"和电子邮件"会将字符串数组发送到目标.这就是我要的.我将标题和内容值存储到一个表中,并将名称/电子邮件组合另存为不同表中的行.

So 'title' and 'content' are just string values, but 'name' and 'email' send an array of strings to the destination. This is what I want. I will store the title and content values into a table, and save the name/email combinations as rows in a different table.

如果我以标准方式(仅通过在form标记中指定"action"参数)发布此表单,则这些值将发送到ASP.NET页,并且可以作为字符串和字符串数组进行处理,理论上它们可以可以很容易地插入数据库中.

If I post this form in the standard way (just by specifying an 'action' parameter in the form tag) then the values are sent to an ASP.NET page and can be processed as strings and string arrays and in theory they can be quite easily inserted into a database.

但是,该应用程序不允许以传统方式提交表单.相反,我必须使用jQuery AJAX发布表单.

However, the application won't allow the form to be submitted in the traditional way. Instead I must use jQuery AJAX to post the form.

问题是,如何使用jQuery AJAX复制传统表单帖子的行为,维护数组值?

The question is, how do I replicate the behaviour of a traditional form post, maintaining array values, using jQuery AJAX?

我知道可以序列化表单值,但是随后我需要做一些服务器端逻辑来反序列化并有效地创建数组,这是一个额外的步骤.

I know it's possible to serialize the form values but then I need to do some server-side logic to deserialize and effectively create the array, which is an extra step.

有没有人知道一种方法来复制通过jQuery AJAX传递字符串数组时与执行标准表单发布时相同的行为?

Does anyone know of a way to just replicate the same behaviour in passing a string array through jQuery AJAX as when doing a standard form post?

谢谢大家!

推荐答案

您可以使用jQuery的 $ .serialize $ .serializeArray 来序列化表单字段,以便像下面这样使用ajax发送表单字段

You can use jQuery's $.serialize or $.serializeArray to serialize form fields to send them using ajax like following

$.post('URL',$('form').serialize(),function(data){
     // success
})

有关 $ .post

这篇关于通过jQuery发布数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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