一個字符串我有一個HTML輸入此代碼自動完成:如何檢查輸入具有在JavaScript
$("#myinput")
.bind("keydown", function(event) {
// don't navigate away from the field on tab when selecting an item
if (event.keyCode === $.ui.keyCode.TAB
&& $(this).data("autocomplete").menu.active) {
event.preventDefault();
}
})
.autocomplete({
minLength: 0,
source: function(request, response) {
var results = [],
selectionStart = this.element[0].selectionStart
term = extractLast(request.term.substring(0, selectionStart));
if (term.length > 0) {
console.log(term);
if(/*input has string "where"*/)
results = $.ui.autocomplete.filter(table1, term);
else
results = $.ui.autocomplete.filter(table2, term);
}
response(results);
},
focus: function() {
return false; // prevent value inserted on focus
},
select: function(event, ui) {
var terms = split(this.value.substring(0, this.selectionStart));
terms.pop(); // remove the current input
terms.push(ui.item.value); // add the selected item
this.value =
$.trim(terms.join(" ") + this.value.substring(this.selectionStart)) + " ";
return false;
}
});
如果輸入有字符串我試圖做的是「地方」,在某個地方,那麼它將從table1加載自動完成,否則它將從table2加載。我怎樣才能檢查輸入是否有該字符串?提前致謝。
,你可以使用'檢查typeof' https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/typeof –
你的意思是在你的輸入框中的值? –
@MohideenibnMohammed是的。 – jason