代码之家  ›  专栏  ›  技术社区  ›  Shamoon

是否有jquery自动完成插件可以强制选择项目?

  •  6
  • Shamoon  · 技术社区  · 15 年前

    autocomplete 但它不会强制选择项目。我需要这样的东西,但它必须强制选择一个项目才能“提交”。

    它存在吗?

    6 回复  |  直到 12 年前
        1
  •  2
  •   Patrick Desjardins    14 年前

    这是我的.blur解决方案(而且我使用name id对)

    jQuery("#username").autocomplete(
        "/yii_app/stock/suggestUser",
        {
            'mustMatch':true,
            'multiple':false,
            'formatItem':function(result, i, num, term) {
                return '<small>'+result[1]+'</small>&nbsp;&nbsp;&nbsp;'+ result[0];
            }
        }
    ).result(
        function(event,item) {
            $("#user_id").val(item[1]);
        }
    ).blur(
        function() {
            $("#user_id").val('');
            $(this).search();
        }
    );
    

    用户ID是隐藏的输入字段,用户名是文本输入字段 Ajax结果使用以下格式: user1name user1id\n user2name user2id\n

        2
  •  4
  •   Ben W    12 年前

    你可以使用 "change" event 自动完成

            var availableTags = [
                "ActionScript",
                "AppleScript"
            ];
            $("#tags").autocomplete({
                source: availableTags,
                change: function (event, ui) {
                    if(!ui.item){
                        //http://api.jqueryui.com/autocomplete/#event-change -
                        // The item selected from the menu, if any. Otherwise the property is null
                        //so clear the item for force selection
                        $("#tags").val("");
                    }
    
                }
    
            });
    
        3
  •  1
  •   Peter Mortensen code4jhon    12 年前

    使用选项:

    mustMatch:true,
    

    这在bassistance autocomplete插件中可用。我不知道它是jquery autocomplete的一部分还是bassistance的autocomplete的一部分,但是它可以工作!

        4
  •  1
  •   Jens Sunil goyal    12 年前

    这是我当前使用的解决方案 jQuery UI autocomplete 以下内容:

        $('#some-input').autocomplete({
            source: 'some-url',
            autoFocus: true,
            minLength: 2,
            select: function(event, ui) {
                //remember the selected item
                $('#some-input')
                    .data('selected-item', ui.item.label);
            }
        }).blur(function() {
            var value = $('#some-input').val();
            //check if the input's value matches the selected item
            if(value != $('#some-input').data('selected-item')) {
                //they don't, the user must have typed something else
                $('#some-input')
                    .val('') //clear the input's text
                    .data('selected-item', ''); //clear the selected item
            }
        });
    

    每次触发模糊事件时,都会检查输入的值是否已被选中。如果尚未选取(您知道,因为从未触发选择事件),则'selected item'变量将与输入的值不匹配。然后你可以做你想做的任何事情(我清除输入,但你可以尝试其他事情)。

        5
  •  0
  •   tvanfosson    15 年前

    结合使用 validation 插件。就这么做吧 required 用于验证插件。

        6
  •  0
  •   CamelBlues    14 年前

    您还可以使用内置提交事件

    $('Autocomplete').autocomplete({
              select: function(event, ui) {
                           $('submit').show();
                      }
    });