![](/img/trans.png)
[英]How to check if the user has entered the value of date or not in Javascript?
[英]How to check if user has entered typeahed value
我有以下代碼:
var students = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('fullName'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/api/students?query=%QUERY',
wildcard: '%QUERY'
}
});
$('#student').typeahead({
minLength: 2,
highlight: true,
},
{
name: 'students',
display: function (item) { return item.fullName },
source: students.ttAdapter(),
templates: {
empty: [
'<div class="tt-empty-message">' +
'No results found' +
'</div>'
]
}
}).on("typeahead:select",
function (e, student) {
console.log("inside isselect");
isSelected = true;
vm.studentId = student.id;
});
我正在嘗試捕獲一個場景,如果用戶輸入了一些亂碼或除預輸入值之外的其他內容,然后單擊“提交”按鈕,那么我需要顯示一條錯誤消息。 為此,我需要捕獲提前輸入列表,並查看已提前輸入的列表是否包含輸入值。 我該怎么做?
我已經搜索了此解決方案5個小時了。 我在其他線程上查找並實現了類似的答案,但似乎沒有任何效果。 例如看一下這個答案:
http://jsfiddle.net/Fresh/bbzt9hcr/
我嘗試了baove代碼,但打字錯誤甚至沒有出現。 而我的代碼運行得很好。 請讓我知道我該如何解決。 先感謝您。
有多種方法可以解決這個問題,最簡單的方法是:您可以偵聽typeahead更改事件並使用變量存儲所選值,然后在提交檢查並發送該變量時: http : //jsfiddle.net/alfredopacino/bbzt9hcr/290 / 。
yourTypehead.on('typeahead:selected', function($e, datum) { // suggestion selected
selectedMovie = datum.value
});
$('#submit').click(function() {
if(selectedMovie) {
//your ajax call
} else {
alert("no valid movie")
}
});
這樣做的問題是它不能涵蓋用戶手動輸入有效值的情況。 要解決此情況,您可以采用以下兩種方法:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.