简体   繁体   English

Select2占位符不起作用

[英]Select2 Placeholder not working

I'm trying to use the Select2 multiple placeholder. 我正在尝试使用Select2多个占位符。 And when I use it, its simply not working. 当我使用它时,它根本无法正常工作。

this is my JS code: 这是我的JS代码:

        $(".materialS").select2({
        placeholder: 'Material types'
    });

and the Html/Php code: 以及Html / Php代码:

<select class="materialS" name="material[]" multiple="multiple">
                <?php if ($materials){
                    foreach ($materials as $material) { ?>
                    <option value="<?php echo safe_class($material)?>"><?php echo $material; ?></option>
                    <?php }} ?>
                </select> 

so I get the materials from db(more like wp). 所以我从db获得资料(更像wp)。 now the thing is that when I click on some select and then I remove it, I can see the placeholder but not before. 现在的问题是,当我单击某些选择然后将其删除时,可以看到占位符,但之前看不到。

any idea whats wrong? 你知道怎么了吗?

Dom is not ready when javascript script is executed. 执行JavaScript脚本时,Dom尚未就绪。

try including this code inside document.ready https://learn.jquery.com/using-jquery-core/document-ready/ 尝试在document.ready https://learn.jquery.com/using-jquery-core/document-ready/中包含此代码

$(function() {
    $(".materialS").select2({
        placeholder: 'Material types'
    });
});

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

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