Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Laravel 8从数据库中选择2个多选项_Php_Jquery_Node.js_Laravel_Jquery Select2 - Fatal编程技术网

Php 使用Laravel 8从数据库中选择2个多选项

Php 使用Laravel 8从数据库中选择2个多选项,php,jquery,node.js,laravel,jquery-select2,Php,Jquery,Node.js,Laravel,Jquery Select2,我有个小问题。我的编辑表单中有一个Select2 multiselect字段。现在,我的目标是让脚本加载所有选项,并预选正确的选项 我已经试过了,但到目前为止还没有达到预期的效果 这是我的密码 multiselect的控制器代码段 // Convert multiselect in right form $product = $product['tags']; $new = serialize($user); $product['tags'] = $new; edit.blade.php代码段

我有个小问题。我的编辑表单中有一个Select2 multiselect字段。现在,我的目标是让脚本加载所有选项,并预选正确的选项

我已经试过了,但到目前为止还没有达到预期的效果

这是我的密码

multiselect的控制器代码段

// Convert multiselect in right form
$product = $product['tags'];
$new = serialize($user);
$product['tags'] = $new;
edit.blade.php代码段

<div class="form-group">
    <label for="tag">Product Tags</label>
    <select id="tag" class="select2 select2-hidden-accessible {{ $errors->has('tags') ? 'error' : '' }}" multiple="multiple" name="tags[]" style="width: 100%;" data-select2-id="5" tabindex="-1" aria-hidden="true" value="1">
        @foreach($tags as $tag)
                <option value="{{ $tag->id }}" {{ in_array($tag->id, $prisoner->tag) ? 'selected="selected"' : '' }}>{{ $tag->label }} - {{ $tag->name }}</option>
            @endif
        @endforeach
    </select>
</div>

我希望有人能帮助我。

解决方案示例:带id标签

var values=$'tags option[selected=true]'.mapfunction{return$this.val;}.get; //如果不想触发事件,则不需要.triggerchange $'tags'。选择2{占位符:请选择}; 应用1 球棒 猫 狗1 大象 查看/曝光 查看/曝光 虚拟数据 虚拟数据 虚拟:数据 DummyData
如果我理解得很好,你想在加载页面时看到属性为selected='selected'的预选选项吗?这很有趣,因为我在我的页面上看不到任何东西code@Cookie我不理解你的评论。我尝试了你的解决方案,但我得到了以下结果:所以我不知道为什么。有两种方法。首先,我的代码出错,或者我有一个select2错误。您的程序中的id是tag,而您的select2标记中的id是tag
$('#tag').select2({ placeholder: "Please select" }).trigger('change');