Javascript jquery 序列化和多选下拉菜单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5118716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:44:53  来源:igfitidea点击:

jquery serialize and multi select dropdown

javascriptjqueryformswebforms

提问by Amitabh

I have been using the jquery serialize() function to serialize the values of a form and submit it via ajax

我一直在使用 jquery serialize() 函数来序列化表单的值并通过 ajax 提交它

like for e.g. if the form name and id is factoryUsers

例如,如果表单名称和 id 是factoryUsers

var data=$("#factoryUsers").serialize();

var data=$("#factoryUsers").serialize();

Now this works fine for forms that have text fields, text areas, simple drop downs etc. But when I have a multiple dropdown , things go awry for e.g. if I have a dropdown of the type

现在这适用于具有文本字段、文本区域、简单下拉列表等的表单。但是当我有多个下拉列表时,事情就会出错,例如,如果我有一个类型的下拉列表

<select size="5" id="factoryUsers" name="factoryUsers" multiple="multiple">

<select size="5" id="factoryUsers" name="factoryUsers" multiple="multiple">

the serialize doesn't work correctly anymore. so if I select 3 users I get a query string like

序列化不再正常工作。所以如果我选择 3 个用户,我会得到一个查询字符串

factoryUsers=5&factoryUsers=23&factoryUsers=11

factoryUsers=5&factoryUsers=23&factoryUsers=11

changing the select to array type doesn't help either factoryUsers[]

将选择更改为数组类型也无济于事 factoryUsers[]

Any idea or help how to get this working correctly would be great.

任何想法或帮助如何使其正常工作都会很棒。

采纳答案by Jordan

The string output you've described above is the correct way of submitting multiple values for forms with the same name over HTTP, so jQuery is working correctly. It's up to you to handle how this is processed on the server-side, which is then dependent on what language you are using.

您上面描述的字符串输出是通过 HTTP 为具有相同名称的表单提交多个值的正确方法,因此 jQuery 工作正常。由您来处理如何在服务器端进行处理,这取决于您使用的语言。

If you're using PHP, this may help: http://bytes.com/topic/php/answers/12267-how-php-_post-gets-multiple-values-html-form

如果您使用 PHP,这可能会有所帮助:http: //bytes.com/topic/php/answers/12267-how-php-_post-gets-multiple-values-html-form

Can you tell us what language you're using?

你能告诉我们你用的是什么语言吗?

回答by TJHeuvel

Try changing the name of the select to factoryUsers[]. That way you can loop through it in your backend.

尝试将选择的名称更改为factoryUsers[]. 这样你就可以在后端循环遍历它。

回答by Andris Polnikovs

All you need is change name="factoryUsers" to name="factoryUsers[]" PHP will treat it as array.

您只需要将 name="factoryUsers" 更改为 name="factoryUsers[]" PHP 会将其视为数组。

回答by akdora

Just try it to get value with val()

尝试用 val() 获取价值

data='factoryUsers=' + $("#factoryUsers").val();

It will give you the values with comma seperate.

它将为您提供逗号分隔的值。

factoryUsers=1,2,4

工厂用户=1,2,4

If this item is one of your form. Then try to add the value of item end of the serialize. Example:

如果此项目是您的表格之一。然后尝试添加序列化项目结束的值。例子:

data= f.serialize() + '&factoryUsers=' + $('#factoryUsers').val();

Since, the item written twice into the request, the last value will be valid in controller.

由于项目两次写入请求,最后一个值将在控制器中有效。

回答by Ferhat KO?ER

Your elements name must be array type factoryUsers[]Change your code this:

您的元素名称必须是数组类型factoryUsers[]更改您的代码:

<select size="5" id="factoryUsers" name="factoryUsers[]" multiple="multiple">

Thanks...

谢谢...

回答by leocoder

You can use the PHP function

你可以使用PHP函数

parse_str($_POST)

This function seems to be an extract function. After you run this, you can access a var named $factoryUsers, and this var is an array $factoryUsers[n].

这个函数似乎是一个提取函数。运行此程序后,您可以访问名为 的变量$factoryUsers,该变量是一个数组$factoryUsers[n]