【问题标题】:jquery ui :Force selection of autocompletejquery ui:强制选择自动完成
【发布时间】:2014-05-29 14:50:05
【问题描述】:

好的,这是我的original question,但我觉得我需要问一个单独的问题,因为我没有完全理解我想要完成的事情

我正在使用 jquery Tag-it 插件https://github.com/aehlke/tag-it,并且需要将这些操作与数据库插入/更新/删除联系起来

我试图阻止的是当用户开始输入并且没有选择 jquery ui 自动完成项并通过 enter 或 tab 键或鼠标单击来集中注意力时,仍然会为用户输入的任何内容创建一个标签...我希望用户只能从jquery ui自动完成列表中选择现有标签,没有其他操作可以创建新标签,新标签将单独创建

在这里查看我的代码http://jsfiddle.net/jrizzi1/2wjKR/2/

Tag-It 函数具有带有 pre 和 post 事件的方法,例如:CreateTag 方法具有 beforeTagAdded 和 AfterTagAdded 事件。

我使用 ajax $.post 在 beforeTagAdded 事件中发布了我的数据库插入,但是因为标签不必存在,我得到了这些的孤立行(entityTags 对用户有标签“go”,标签表有没有名为“go”的标签)

我认为我可以先发布 ajax 帖子,然后尝试在从表中插入 select 并将 false 返回到 BeforeTagAdded 事件,但是 ajax 是异步的,正如下面所有善良的人所指出的那样,beforeTagAdded 在ajax

我还在自动完成小部件上尝试了一些不同的东西,例如

        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
                $(".tagit-new input").val("");
            }

这可以防止模糊创建,但 enter 和 tab 键仍然允许不选择自动完成项

下面是我所有的代码,它使用 jquery > 1.5.2 jquery ui 1.8.12, tag-it 2.0

var entity_id = getParameterByName('idnumber');


function dotagit(){
        $("#myTags").tagit({
//removeConfirmation: true,         
 beforeTagAdded: function(event, ui) { //before the tag is added to the client, NOT the database    
     if ( !ui.duringInitialization){
         //console.log(ui);              

            $.ajax({
                    url: "handlers/tags.ashx",
                    dataType: "json",
                    data: {
                        idnumber: entity_id,
                        tag: ui.tagLabel,
                        operation:"tag"
                    }
                }).done(function(data){

                    if(data.result == false){
                     return false;
                    }
                }).complete(function(data){

                });

         }
 },
 afterTagAdded: function(event, ui) {

    if ( !ui.duringInitialization){
        console.log('after'); 
         }
    },      

 beforeTagRemoved: function(event, ui) {

        $.ajax({
                    url: "handlers/tags.ashx",
                    dataType: "json",
                    data: {
                        idnumber: entity_id,
                        tag: ui.tagLabel,
                        operation:"removetag"
                    }
                }).done(function(data){
        //console.log(data);    
            });
    },
 afterTagRemoved: function(event, ui) {
 },

onTagExists: function(event, ui) {
        // do something special
        console.log(ui.tag);
        console.log('onTagExists');     
    },          

 autocomplete: { source: function( request, response ) {
                $.ajax({
                    url: "handlers/tags.ashx",
                    dataType: "json",
                    data: {
                        idnumber: entity_id,
                        q: request.term,
                        operation:"query"
                    }
                }).done(function(data){
        //console.log(data);
                        response( $.map( data.data, function( item ) {
                            return {
                                label: item.NAME + " x " + item.COUNT,  // + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                                value: item.NAME
                            }
                        }));        
                })
            },
            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
                    $(".tagit-new input").val("");
                }

            }, 

            minLength: 0
                    }
                });         
    };


$(document).ready(function() {


$.ajax({  
  type: "GET",  
  url: "handlers/tags.ashx",
  data: {
          idnumber: entity_id,
          operation:"get"
                    }
  }).done(function(data){
        //console.log(data);
        $.each(data.data, function(key, value){
            $('#myTags').append('<li>'+value.TAG+'</li>');
            });

  }).fail(function(xhr, err){   
        var responseTitle= $(xhr.responseText).filter('title').get(0);
        alert($(responseTitle).text() + "\n" + formatErrorMessage(xhr, err) ); 
  }).complete(function(){
        dotagit();  
  });
});

【问题讨论】:

    标签: javascript jquery jquery-ui autocomplete tag-it


    【解决方案1】:

    使用 tag-it,如果不修改源代码,我认为这是不可能的。 (我愿意在这里更正!)如果您可以灵活地实施,我推荐jQuery TokenInput,默认情况下可以这样做。 (设置allowFreeTagging: false)。

    无论如何,关于修改 tagit 源。 blur-add 和 enter/tab-add 分开处理。

    Enter/Tab 添加是第 269-272 行

     if (!(that.options.autocomplete.autoFocus && that.tagInput.data('autocomplete-open'))) {
                                    that.tagInput.autocomplete('close');
                                    that.createTag(that._cleanedInput());
    }
    

    模糊添加是第 277-9 行

    if (!that.tagInput.data('autocomplete-open')) {
                            that.createTag(that._cleanedInput());
                        }
    

    现在,我相信如果您注释掉这两个createTag 行,那么您将拥有只能通过单击列表中的标签来添加标签的行为。 (我相信这是在第 286 行处理的。)或者,如果您仍然希望能够在 blur/enter 上添加标签,仅当它们在数据库中时,您可能希望在此时插入您的 AJAX 调用 - 并且将 createTag 行添加到 AJAX 的 done 块。

    (行号参考v2.0,当前GitHub Version。)

    【讨论】:

    • 我的想法相同,并在第 246 行更改了源代码,我将保持问题开放,以便有人可以证明我们错了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    相关资源
    最近更新 更多