2013-08-05 81 views

回答

0

我想我設法做到了,但是這基本上是恢復Autosuggests在選擇某個項目時所做的一切。

一下添加到選項哈希:

selectionAdded: function(elem) { 
    var in_the_result_list = $.grep($('.as-results .as-list .as-result-item'), function(item) { return $(item).text() === elem.textNotChild() }).length; 
    if (!in_the_result_list) { 
    elem.remove(); 
    $('.as-input').html(elem.textNotChild()); 
    $('.as-values').val(function(i, current_value) { 
     return current_value.replace(elem.textNotChild(), ''); 
    }); 
    } 
} 

也許有更好的方法來做到這一點?我使用自定義jquery方法來獲取沒有「x」的元素的文本,隨時以您想要的方式實現這一點。以下是您需要的方法:

(function ($) { 
    function elementText(el, separator) { 
    var textContents = []; 
    for(var chld = el.firstChild; chld; chld = chld.nextSibling) { 
     if (chld.nodeType == 3) { 
     textContents.push(chld.nodeValue); 
     } 
    } 
    return textContents.join(separator); 
    } 
    $.fn.textNotChild = function(elementSeparator, nodeSeparator) { 
    if (arguments.length<2){nodeSeparator="";} 
    if (arguments.length<1){elementSeparator="";} 
    return $.map(this, function(el) { 
     return elementText(el,nodeSeparator); 
    }).join(elementSeparator); 
    } 
} (jQuery));