通过jQuery发布数组

编程入门 行业动态 更新时间:2024-10-26 20:31:29
本文介绍了通过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发布数组

本文发布于:2023-10-12 02:51:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483540.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!