laravel Select2 - 多个标签不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32028526/
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-09-14 12:11:48  来源:igfitidea点击:

Select2 - Multiple tags not working

javascriptphpjquerylaraveljquery-select2

提问by noobmaster69

I'm trying to use Select2 (https://select2.github.io) to allow a user to type multiple tags into a field before submitting a form. In my Laravel PHP app, I'll then take those tags, determine if they exist and add them into a database.

我正在尝试使用 Select2 ( https://select2.github.io) 来允许用户在提交表单之前在字段中键入多个标签。在我的 Laravel PHP 应用程序中,我将获取这些标签,确定它们是否存在并将它们添加到数据库中。

My problem is that I can't seem to get Select2 to recognise there are multiple tags being entered by the user. When I interrogate the form data, I only see the LAST tag a user typed as opposed to ALL the tags.

我的问题是我似乎无法让 Select2 识别用户输入了多个标签。当我查询表单数据时,我只看到用户输入的最后一个标签,而不是所有标签。

My Select2 element is:

我的 Select2 元素是:

<select class="tags-field" name="tags" data-tags="true" data-placeholder="TAGS" multiple="multiple">
</select>

and my JQuery is:

我的 JQuery 是:

$(function() {
    $(".tags-field").select2({
        maximumSelectionLength: 3,
        tokenSeparators: [','],
    });
}

There are no Javascript errors and it works perfectly fine except I cannot detect ALL the tags.

没有 Javascript 错误,它工作得很好,只是我无法检测到所有标签。

回答by Paul Vidal

To cause PHP to make all the selected choices available as an array, suffix your select name with a pair of square brackets, like this:

要使 PHP 将所有选定的选项作为数组可用,请在您的选择名称后加上一对方括号,如下所示:

<select class="tags-field" name="tags[]" data-tags="true" data-placeholder="TAGS" multiple="multiple">

If this form is sent to a PHP program, the value of $_POST['tags'] will be an array. Note that the square brackets in the form control name aren't a part of the array key. You would process such a form like this:

如果将此表单发送到 PHP 程序,则 $_POST['tags'] 的值将是一个数组。请注意,表单控件名称中的方括号不是数组键的一部分。您将处理这样的表单:

<?php
$tags = $_POST['tags'];
// Note that $tags will be an array.
foreach ($tags as $t) {
    echo "$t<br />";
}
?>

References here: http://bbrown.kennesaw.edu/papers/php2.html

参考资料:http: //bbrown.kennesaw.edu/papers/php2.html

回答by dm4web

  1. use hidden input field in order to send all values
  2. use onsubmit event to set the value of the hidden field
  1. 使用隐藏输入字段以发送所有值
  2. 使用 onsubmit 事件设置隐藏字段的值

HTML:

HTML:

<form method="post" action="post.php">
    <select multiple id="e1" style="width:300px" name="_state">
        <option value="AL">Alabama</option>
        <option value="Am">Amalapuram</option>
        <option value="An">Anakapalli</option>
        <option value="Ak">Akkayapalem</option>
        <option value="WY">Wyoming</option>
    </select>
    <input type="hidden" name="state" value="" />
    <input type="submit"/>
</form>

JQ:

江青:

$("#e1").select2();

$('form').submit(function () {
    var newvalue = '';
    var value = $('select[name="_state"]').val();
    if (value) {
        newvalue = value;
    }
    $('input[name="state"]').val(newvalue);
})