简体   繁体   English

如何更改select2中的占位符?

[英]How to change placeholder in select2?

How do I change the data placeholder with select2?如何使用 select2 更改数据占位符?

So far I've tried this.到目前为止,我已经尝试过这个。

$("#city").select2({placeholder:"foo"});

And this...而这...

$("#city").attr("data-placeholder","bar");

But neither works.但两者都不起作用。

I found that if I just set the attr eg $("#city").attr('data-placeholder', 'bar') , there is no effect.我发现如果我只是设置 attr 例如$("#city").attr('data-placeholder', 'bar') ,则没有效果。 However, if I set the attr and then call $("#city").select2() with no arguments, then the placeholder updates.但是,如果我设置 attr 然后不带参数调用$("#city").select2() ,则占位符会更新。

eg例如

$("#city").attr("data-placeholder","bar");
$("#city").select2();

For other people looking for an attribute solution to avoid changing JS code.对于其他正在寻找属性解决方案以避免更改 JS 代码的人。 I just had to look this up myself for a project, and it seems select2 just needs the attribute data-placeholder="".我只需要自己查找一个项目,似乎 select2 只需要属性 data-placeholder=""。

<select id="city" data-placeholder="my placeholder"></select>

See more here: select2 options reference在此处查看更多信息: select2 选项参考

You can also do it like this in the template (html) level.您也可以在模板 (html) 级别这样做。 Just be sure to assign a blank (eg "") string on your first tag.请务必在您的第一个标签上分配一个空白(例如“”)字符串。

<select id="city" data-placeholder="Select Anything">
  <option value=""></option>
  <option value="option1">Option 1</option>
  <option value="option2">Option 2</option>
</select>

or we can simply add a data attribute for placeholder via javascript and it should happen before select2 gets initialized.或者我们可以简单地通过javascript为占位符添加一个数据属性,它应该在select2初始化之前发生。

$('#city').data('placeholder','This is a placeholder');

DEMO演示

A more direct way ..更直接的方法..

$('#select2-' + $id + '-container').find('.select2-selection__placeholder').text('Your Text');

where $id is the id of the select element其中$id是 select 元素的id

A bit more of a hack-y solution, but one that doesn't involve re-initiating the entire select element is to add the following extensions:更多的hack-y解决方案,但不涉及重新启动整个select元素的解决方案是添加以下扩展:

$.fn.getSelect2Placeholder = function () {
    var $select2Container = $(this).data('select2').$container;
    return $select2Container.find('.select2-selection__placeholder').text();
};
$.fn.setSelect2Placeholder = function (placeholder) {
    var $select2Container = $(this).data('select2').$container;
    return $select2Container.find('.select2-selection__placeholder').text(placeholder);
};

This allows for updating the placeholder by simply writing:这允许通过简单地编写来更新占位符:

$('#the-id-of-your-select2-element').setSelect2Placeholder("Your placeholder text.");

Source 来源

把它放在你的定位器使用的 html 中,如下所示:

<select id="city" placeholder="my placeholder"></select>

对于 select2 版本 4,我在 js init 代码中指定了占位符文本,然后使用$select.select2{placeholder:"updated text..."}更新它

可以使用以下脚本:

$("#city").attr('placeholder', 'your text here' );

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

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