我具有交點功能通過輸入被饋送<form>
的Javascript ::防止當頁面被加載
var chatArr = [];
function input() {
var chat = document.getElementById("yousay").value;
chatArr.push(chat);
var words = chatArr.join(' ').match(/\S+/g);
if(chat.length == 0){
console.log("Mithras hears you")
}
else if(words.length > 1){
return words;
}
else{
console.log(chat)
};
}
然後未被捕獲的錯誤:
function setIntersection(a, b) {
var result = [];
for (var i = 0; i < a.length; i++) {
if (b.indexOf(a[i]) !== -1 && result.indexOf(a[i]) === -1) {
result.push(a[i]);
}
}
return result;
}
其中,在其轉,進入到該功能:
Song.prototype.lyricsIntersect = function(input) {
var bestSong = null;
var bestCount = -Infinity;
for (var i in songs) {
var currentCount = setIntersection(songs[i].lyrics, input()).length;
(...)
當頁面被加載時,表單顯然是空的,所以我得到:
Uncaught TypeError: Cannot read property 'indexOf' of undefined
如何防止這種錯誤加載頁面時?
'函數input'必須返回一個數組的每一個值。 'b.indexOf'什麼? – PHPglue
該計劃的目的是什麼? – PHPglue