【问题标题】:JQuery UI Autocomplete Combobox highlightJQuery UI 自动完成组合框高亮
【发布时间】:2015-10-01 12:37:36
【问题描述】:

我有一个<select>,我需要将其转换为功能更强大的组合框。

<span class="combo_box">
    <select id="state" name="state" tabindex="24">
        <option value="" disabled selected style="display:none"></option>
        <!-- Some JSP here -->
    </select>
</span>

我把它转换成小部件(所以原来的&lt;select&gt; id "state" 仍然存在但隐藏)像这样$("#state").combobox(); 我需要的是在表单提交时向这个自定义小部件添加一个错误突出显示如果组合框是空的。 jQuery UI 有一个自定义 CSS 类 ui-state-error

验证脚本(在我调试时工作正常)

function setComboboxClassBeforeSubmit(elementID) {
    var flag = true;
    var element = document.getElementById(elementID);
    var combobox = $(element).combobox();
    combobox.removeClass('ui-state-error');
    if (isEmpty(element)) {
        combobox.addClass('ui-state-error');
        flag = false;
    }
    return flag;
}

但是添加这个 CSS 类似乎对小部件的外观没有影响。还尝试了像here 显示的硬编码,$("#state").addClass('ui-state-error') 之类的东西,但仍然没有运气。 还尝试了this solution,对选择菜单效果很好,但对组合框效果不佳。

可能的解决方案:(感谢nowhere

像这样编辑_createAutocomplete: function()

_createAutocomplete: function() {
            var selected = this.element.children( ":selected" ),
                value = selected.val() ? selected.text() : "";

        this.input = $( "<input>" )
            .appendTo( this.wrapper )
            .val( value )
            .attr( "title", "" )
            .attr( "id", "ui-" + this.element.attr("id") + "-input" )
...

问题是 - 默认 API 不会为组合框的元素生成唯一 ID。

【问题讨论】:

  • 你确定你没有将类应用到隐藏的选择吗? :P
  • @nowhere 这是问题的核心,在我提到的工作解决方案中,他们为小部件元素使用了一些生成的 id。但我没有找到组合框元素(自动完成和按钮)的 ID。
  • 另外:据我所知 style="display:none" 在添加到选项时并不适用于所有浏览器。你用旧版本的 IE 试过吗?
  • 此解决方案适用于特定浏览器和特定客户,因此没有问题。
  • 您需要以某种方式识别小部件创建的选择...您不能依赖任何类或 id 吗?它是否有可以用来识别它的父元素?

标签: javascript jquery css jquery-ui


【解决方案1】:

你能试试我在这里写的代码吗?我使用jqueryui示例进行操作:http://jsfiddle.net/b5e83x2q/

function checkValue(item){
    item.removeClass('ui-state-error');
    if(item.val()==""){
        item.addClass('ui-state-error');
    }
}

checkValue($(".custom-combobox input"));
.ui-state-error{
    background-color:red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="custom-combobox">
    <input title="" class="custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left ui-autocomplete-input" autocomplete="off" value="">
</span>

编辑:

如果你需要识别一个特定的选择,你应该给它一个特定的 id:如果你使用与 jqueryui 示例相同的代码,你应该有:

  _create: function() {
    this.wrapper = $( "<span>" )
      .addClass( "custom-combobox" )
      .insertAfter( this.element );

    this.element.hide();
    this._createAutocomplete();
    this._createShowAllButton();
  }

你可以尝试添加类似的东西:

this.attr('id', 'specificId');

或编辑跨度包装器:

_create: function() {
    this.wrapper = $( "<span>" )
      .addClass( "custom-combobox" )
      .insertAfter( this.element )
      .attr('id', 'specificId');

    this.element.hide();
    this._createAutocomplete();
    this._createShowAllButton();
  }

之后你可以使用:

checkValue($("#specificId input"));

只更改 1 个选择。

【讨论】:

  • 问题是如果我对这个类有多个选择,所有这些都会被突出显示。所以我需要提取特定组合框输入和特定组合框按钮的 id。
  • @Ahnassi 是的,但是如果你通过代码向它添加一个 id,你应该能够访问你想要的元素。您在“编辑”部分之后尝试过我的代码吗?
  • 谢谢!编辑 _create: function() 效果很好,虽然我不认为更改 API 是一个很好的解决方案,但我现在拥有的最好的解决方案。
  • @Ahnassi 好吧,我们只是绕过它的限制以使其变得更好:P 很高兴能提供一些帮助。
猜你喜欢
  • 2011-04-07
  • 2011-11-16
  • 2012-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-01
  • 1970-01-01
相关资源
最近更新 更多