简体   繁体   English

如何更改selectize.js下拉列表的占位符?

[英]How to change placeholder of selectize.js dropdown?

I want to change placeholder of a dropdown created by selectize.js when the parent dropdown changes its selection to load the options of the dropdown whose placeholder to be changed. 当父下拉列表更改其选择以加载要更改其占位符的下拉列表的选项时,我想更改selectize.js创建的下拉列表的占位符。 There is no method to do this in documentation. 在文档中没有方法可以做到这一点。

You can specify a placeholder key as part of the options object when initialising. 初始化时,可以将placeholder键指定为选项对象的一部分。 I couldn't find the option in the documentation and only found it digging through the code. 我在文档中找不到该选项,只发现它在挖掘代码。

//init selectize
$(function() {
  $('select').selectize({
    placeholder: 'Click here to select ...',
  });
});

You need two things: 你需要两件事:

  • add empty <option></option> as first option tag in the select. 添加空<option></option>作为select中的第一个选项标记。
  • add placeholder key to selectize call 添加placeholder键以selectize调用

Not sure if selectize keeps changing their code or if everyone else on this thread just whiffed but all of these answers seem to be wrong individually, but if you kind of combine the correct parts from each answer you wind up with this which works for me. 不确定选择是否继续改变他们的代码,或者如果这个帖子上的其他人都只是嗤之以鼻,但所有这些答案似乎都是错误的,但是如果你把每个答案中的正确部分结合起来,你最终会对我有用。

        var textHandler = function(name) {
        return function() {
            if(name == 'focus'){                    
                jQuery("select#myid + .selectize-control").find("input:text").prop({"placeholder": ""});
            }
        };
    };
    jQuery('#sf159_textsearchtextsearch').selectize({
        closeAfterSelect: true,
        hideSelected    : true,
        createOnBlur    : true,
        openOnFocus     : true,
        maxOptions      : 10,
        persist         : true,
        placeholder     : "Neighborhood, Street, School, Zip, MLS",
        plugins         : ['remove_button'],
        valueField      : 'id',
        labelField      : 'text',
        searchField     : 'value',
        options         : [],
        create          : false,
        render          : {
            option: function (item, escape) {
                //console.log(item);
                var address = '';
                var keyword = '';
                var tx = item.text.split(',');
                for (var i = 0, n = tx.length; i < n; i++) {                        
                    address += '<span class="span4">' + escape(tx[i]) + '</span>';                      
                }
                var template = '<div>' +
                        '<div class="row-fluid"> ' + address + ' </div>' +
                        '</div>';
                return template;
            }
        },
        load            : searchHandler,
        onKeydown       : keyHandler,
        onDelete        : deleteHandler,
        onFocus         : textHandler('focus'),
    });

You can specify placeholder for select elements by adding an empty option element inside select tag. 您可以通过在select标记内添加空option元素来为select元素指定占位符。 Here is an example: 这是一个例子:

<select name="color" required>
    <option value=""> Select a color </option>
    <option value="1"> Lavender </option>
    <option value="2"> Eggplant </option>
    <option value="3"> Cool grey </option>
    <option value="4"> Bitter lemon </option>
</select>

Make sure you use a select tag for selectize . 请确保您使用的是select标记为selectize
I was having the same problem, caused by using an input instead. 我遇到了同样的问题,而不是使用输入。 selectize worked too, but the placeholder attribute wasn't being displayed. selectize工作,但placeholder属性没有显示。 When I changed to a select it started working 当我改为select它开始工作

<select type="text" placeholder="Type words of the article..."></select>
$('select#my-dropdown + .selectize-control').find('.selectize-control-input').prop({'placeholder': 'Placeholder Text'});

selectize.js will create an input below the select . selectize.js将在select下面创建一个input This input will have the class .selectize-control . input将具有类.selectize-control

You can replace the placeholder of this input field with jQuery. 您可以使用jQuery替换此input字段的placeholder

You can do something like: 你可以这样做:

$(".selectize-control").attr('placeholder','Hello World!');

If you would like a working example, Ill need more information about your code. 如果您想要一个有效的示例,我需要有关您的代码的更多信息。

I had a similar issue - what was frustrating is I would do something like this: 我有一个类似的问题 - 令人沮丧的是我会做这样的事情:

$("selectize-input input[placeholder]").attr('placeholder','Hello World!');

Then only after changing the focus it would render my new placeholder text. 然后,只有在更改焦点后,它才会呈现我的新占位符文本。 It turns out that for whatever reason (probably a good one) selectize applies a width to this input. 事实证明,无论出于何种原因(可能是一个好的),选择都会对此输入应用宽度。

Final solution: 最终解决方案

$(".selectize-input input[placeholder]").attr('placeholder','Hello World!');
$(".selectize-input input[placeholder]").attr("style", "width: 100%;");

You can update the placeholder by setting selectize.settings.placeholder and then calling selectize.updatePlaceholder() . 您可以通过设置selectize.settings.placeholder然后调用selectize.updatePlaceholder()来更新占位符。

 $(document).ready(function() { var s = $('#input-tags').selectize({ persist: false, createOnBlur: true, create: true, placeholder: 'start placeholder' })[0].selectize; $('#updatePlaceholder').click(function() { s.settings.placeholder = 'new placeholder'; s.updatePlaceholder(); }); }); 
 <!DOCTYPE html> <html> <head> <script data-require="jquery@3.1.1" data-semver="3.1.1" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <link rel="stylesheet" href="style.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.4/css/selectize.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.4/css/selectize.default.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.4/js/standalone/selectize.js"></script> <script src="script.js"></script> </head> <body> <h1>Selectize setting placeholder</h1> <input type="text" id="input-tags" value=""> <button id="updatePlaceholder">change</button> </body> </html> 

This is what worked for me (selectize version "selectize": "^0.12.4" and using typescript only): 这对我有用(选择版本"selectize": "^0.12.4"并仅使用打字稿):

this.selectize = $(this.select).selectize({
  options: this.options,
  placeholder: 'My Placeholder'
})[0].selectize;

this.selectize.settings.placeholder = 'Another Placeholder';
this.selectize.updatePlaceholder();

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

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