简体   繁体   English

JavaScript Multi Select Box未发布所有项目

[英]JavaScript Multi Select Box not posting all items

I found a nice demo on an old JSFIddle for Moving items from one multi-select box to another with JavaScript 我在一个旧的JSFIddle上找到了一个不错的演示,可以通过JavaScript将项目从一个多选框移到另一个

You can see the demo here: http://jsfiddle.net/jasondavis/e6Y7J/25/ 您可以在此处查看演示: http : //jsfiddle.net/jasondavis/e6Y7J/25/

The problem is, the visual part works correctly but when I put this on a server with PHP, it only POST the last item added to the new select box. 问题是,视觉部分工作正常,但是当我把这个用PHP的服务器上,它只是POST的最后一个项目添加到新的选择框。 So instead of POSTING an array of items, it will only POST 1 item regardless of how many items exist in the selection box. 因此,而不是POSTING项目的数组的,它只会POST不论如何选择框中存在许多项目1项。

Can anyone help me? 谁能帮我?

The JavaScript/jQuery JavaScript / jQuery

$(document).ready(function() {
    $('select').change(function() {
        var $this = $(this);
        $this.siblings('select').append($this.find('option:selected')); // append selected option to sibling

    });   
});

I believe I've hit this issue before. 我相信我以前曾遇到过这个问题。 For the PHP $_POST array to populate this correctly you need to add a name field with [] at the end of the name. 为了使PHP $ _POST数组正确填充此名称,您需要在名称末尾添加带有[]的名称字段。 PHP will then interpret the result as an array of all the values and not just the last selected one. 然后,PHP将结果解释为所有值的数组,而不仅仅是最后选择的一个。

Example: 例:

<select name="demo_multi[]" multiple="multiple">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select>

When you recall the item in the $_POST array leave off the square brackets. 当您调用$ _POST数组中的项目时,请不要放在方括号中。

$values = $_POST['demo_multi'];

将多选名称更改为数组

<select name="post_status[]" multiple id="select2" class="whatever" style="height: 500px; width: 222px;"></select>

I think you also have to select all items in the This is pre jquery but it works. 我认为您还必须选择“ This is pre jquery”中的所有项目,但它可以工作。

`<form onsubmit="selectAll();"> ....</form>
function selectAll()
{
    for(j=0; j<document.formdata.elements.length; j++)
    {
    // if a multiple select box then select all items in the box so they are sent with the form
    var currObj = document.formdata.elements[j];
        if (currObj.tagName == 'SELECT' && currObj.multiple == true)
            for (i=0; i<currObj.length; i++)
                currObj.options[i].selected = true;
    }
}`

This will then be loaded into the array named in the 然后将其加载到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM