HTML:对于“选择多个",仅提交一个值,但是.val()返回两个值的数组 [英] Html: For Select multiple, only one value is submitted but .val() returns array of two values

查看:145
本文介绍了HTML:对于“选择多个",仅提交一个值,但是.val()返回两个值的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

渲染页面时,将单选的val()设置为一个值.例如,$("#my_select_box").val(1); 然后,用户在多重选择框中选择一个附加值.

When rendering the page, the val() of a multiple select is set with a single value. For example, $("#my_select_box").val(1); Then the user selects an additional value in the multiple select box.

提交表单时,仅提交新选择的值,而不提交先前设置的值.而在Firefox中进行调试时,.val()函数将返回两个值的数组(前一个值和新选择的值).为什么会这样?

When the form is submitted, only the newly selected value is submitted and not the previously set one. Whereas while debugging in Firefox, the .val() function returns an array of two values (both the previous one and the newly selected one). Why does this happen?

推荐答案

如果您使用PHP编程,则问题就出在这里.使用多选输入时,PHP要求您在输入名称后使用一对空括号.例如:

If you are programming in PHP, the problem lies there. When using multiselect inputs, PHP requires you to have an empty pair of brackets after input name. For example:

<select name="select[]" multiple="multiple">

而不是

<select name="select" multiple="multiple">

如果您无法或不会执行此操作(也许您无权访问基础HTML),则可以选择捕获POST数据,如下所示:

If you can't or won't do this (perhaps you don't have access to the underlying HTML), then you can alternatively capture the POST data like this:

$data_from_post = file_get_contents("php://input");
var_dump($data_from_post);

您将看到可以使用的这样的字符串:

And you'll see a string like this that you can use:

select=one&select=two&select=three&button=submit

这篇关于HTML:对于“选择多个",仅提交一个值,但是.val()返回两个值的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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