2016-07-17 35 views
1

我在我的項目中使用jquery.mentionsInput。該插件在桌面上運行良好,但不適用於移動設備。這是我的代碼。警報()是測試檢查功能是否正常工作。jquery.mentionsInput不能在移動工作

$('textarea.mention').mentionsInput({ 

    onDataRequest:function (mode, query, callback) { 
     alert("To test on mobile"); 
     searchVal = query; 

     $.ajax({ 
     type: "GET", 
     dataType: "json", 
     url: rootPath()+"user/tagFriends/"+searchVal, 

     success: function(data){ 
      data = _.filter(data, function(item) { return item.name.toLowerCase().indexOf(query.toLowerCase()) > - 1 }); 
      callback.call(this, data); 
     } 

     }); 


    } 
    }); 

這是插件link。該插件也不適用於手機。 在此先感謝。對不起,我的英語不好。

回答

0

在您的文件jquery.mentionsInput.js您修改2個功能onInputBoxInput()和onInputBoxKeyPress()這樣的

function onInputBoxInput(e) { 
var ua = navigator.userAgent.toLowerCase(); 
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile"); 
if(isAndroid) {//certain versions of android mobile browser don't trigger   the keypress event. 
if(e.keyCode !== KEY.BACKSPACE) { 
var typedValue = String.fromCharCode(e.which || e.keyCode); //Takes the string that represent this CharCode 
inputBuffer.push(typedValue); //Push the value pressed into inputBuffer 
} 
    } 
updateValues(); 
updateMentionsCollection(); 

var triggerCharIndex = _.lastIndexOf(inputBuffer, settings.triggerChar); //Returns the last match of the triggerChar in the inputBuffer 
if (triggerCharIndex > -1) { //If the triggerChar is present in the inputBuffer array 
    currentDataQuery = inputBuffer.slice(triggerCharIndex + 1).join(''); //Gets the currentDataQuery 
    currentDataQuery = utils.rtrim(currentDataQuery); //Deletes the whitespaces 
    _.defer(_.bind(doSearch, this, currentDataQuery)); //Invoking the function doSearch (Bind the function to this) 
} 
} 

//Takes the keypress event 
function onInputBoxKeyPress(e) { 
var ua = navigator.userAgent.toLowerCase(); 
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile"); 
if(!isAndroid) { 
    if(e.keyCode !== KEY.BACKSPACE) { //If the key pressed is not the backspace 
     var typedValue = String.fromCharCode(e.which || e.keyCode); //Takes the string that represent this CharCode 
     inputBuffer.push(typedValue); //Push the value pressed into inputBuffer 
    } 
} 
} 

,並在您的index.html

在底部添加以下代碼頁面

<script> 


var sendBoxElem = $('textarea.mention'); 
sendBoxElem.bind("input", function(e) { 
var ua = navigator.userAgent.toLowerCase(); 
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile"); 
if(isAndroid) { 
var char = this.value.charCodeAt(this.value.length - 1); 
//$scope.data = char; 
if(e.keyCode === undefined){ 
e.keyCode = char; 
} 
return true; 
} 
}); 

</script> 

請告訴我,如果這是工作

+0

謝謝你,但我做到了已經和我現在忙於另一個項目。如果我將來使用它,我會通知您 –

+0

該解決方案僅在文本結尾處出現「@」時才起作用。正確? – Ankit