JQuery ui selectmenu占位符不起作用

时间:2016-09-01 12:19:22

标签: jquery jquery-ui

我添加了Jquery ui selectmenu占位符无法正常显示错误

$.widget( 'app.selectmenu', $.ui.selectmenu, {
    _drawButton: function() {
        this._super();
        var selected = this.element
                .find( '[selected]' )
                .length,
            placeholder = this.options.placeholder;

        if (!selected && placeholder) {
            this.buttonText.text( placeholder );    
        }
    }
});

$('select').selectmenu({
    placeholder: 'Select a speed'
});

错误是text is not defined。我正在使用Jquery UI版本1.12.0

1 个答案:

答案 0 :(得分:2)

而不是:

this.buttonText.text( placeholder );  

尝试:

this.buttonItem.text(placeholder);

buttonText确实存在:你只有button和buttonItem,你需要buttonItem来改变文本。

示例:

$.widget( 'app.selectmenu', $.ui.selectmenu, {
  _drawButton: function() {
    this._super();
    var selected = this.element
    .find( '[selected]' )
    .length,
        placeholder = this.options.placeholder;

    if (!selected && placeholder) {
      this.buttonItem.text(placeholder);
    }
  }
});
$(function () {
  $('#speed').selectmenu({ placeholder: 'Select a speed' });
});
<link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>

<label for="speed">Select a speed</label>
<select name="speed" id="speed">
    <option>Slower</option>
    <option>Slow</option>
    <option>Medium</option>
    <option>Fast</option>
    <option>Faster</option>
</select>