2016-10-15 23 views
0

所以我很喜歡這整個編碼的事情,並且在完成了一些培訓並開始瞭解JavaScript,Html和CSS之後,我爲自己設定了一個挑戰。這是爲了讓Batcomputer/Jarvis管理我的谷歌日程,遊戲,程序等等。但爲此,我需要搜索HTML文本輸入的單詞,然後啓動相應的功能。html文本來選擇一個JavaScript函數

下面是一些重要的代碼部分:

<input type="text" id="inputField" onchange="insertInput()"></input> 

<script> 
var currentInput = 1; 
//The input and function picker 
function insertInput() { 
    document.getElementById("inputHtml").innerHTML = currentInput; 
    console.log(currentInput); 
    var containsLaunchGame = /launch game/g; 
    var resultLaunchGame = currentInput.test(containsLaunchGame); 
    console.log(resultLaunchGame); 

    if (resultLaunchGame = true) { 
    scanGames(); 
    console.log("I found Launch Game"); 
    }; 
}; 
</script> 

這還不是全部的代碼,但只是部分我有問題的。 除了string.test之外,我也嘗試過string.search和其他幾個,但沒有一個能夠工作,我希望它們如何。

我要做什麼(其實並不重要)是我可以告訴我的電腦根據我輸入的內容做一些事情。但是我嘗試過的所有方法都會導致錯誤。

對於上面的錯誤代碼是「Uncaught TypeError:currentInput.test不是函數」。

如果您有任何解決方法,無論如何,那麼我會很感激!

回答

0

你的代碼中有很多語法錯誤。請參閱內嵌評論以瞭解修補程序。你應該看看你的瀏覽器控制檯。使用Chrome開發工具,我們擁有非常強大的調試工具。

var currentInput = 1; 
 

 
function insertInput() { 
 
    // assigning current input was backwards, and you should use `value` for inputs 
 
    currentInput = document.getElementById("inputField").value; 
 
    console.log(currentInput); 
 
    var containsLaunchGame = /launch game/g; 
 
    // the regex test is also backwards. test is a method on a regex not a string 
 
    var resultLaunchGame = containsLaunchGame.test(currentInput); 
 
    console.log(resultLaunchGame); 
 
    // in the if statement you were using a single equals, which is the 
 
    // assignment operator, == or === test for equality 
 
    if (resultLaunchGame === true) { 
 
    // scanGames(); 
 
    console.log("I found Launch Game"); 
 
    }; 
 
};
<input type="text" id="inputField" onchange="insertInput()" />

0

的測試方法屬於RegExp對象和接受的字符串作爲PARAM待測試...

嘗試:

containsLaunchGame.test(currentInput); 
0

.test不是string的函數,但是Regexp代替。

所以使用

var resultLaunchGame = containsLaunchGame.test(currentInput); 

此外,在控制檯日誌顯示"I found Launch Game"以大寫字母,你的正則表達式/launch game/g不佔它。如果您想不區分大小寫,請改用/launch game/gi

1

你用「currentInput.test(containsLaunchGame)」調用的東西;「部分實際上是var currentInput = 1; => 1.test()

替換此行:

document.getElementById("inputHtml").innerHTML = currentInput; 

與:

currentInput = document.getElementById("inputHtml").innerHTML; 

此後更換

var resultLaunchGame = currentInput.string.indexOf(containsLaunchGame) !== -1; 
console.log(resultLaunchGame); 

if (resultLaunchGame = true) { 

var resultLaunchGame = currentInput.test(containsLaunchGame); 
console.log(resultLaunchGame); 

if (resultLaunchGame == true) { 
0

currentInput是一個數字,而不是一個繩子,讓你無法使用像.search

字符串方法試着做currentInput一個字符串,並使用.search方法

另外,你的例子沒有包含id爲「inputHtml」的元素。

代替onchange嘗試使用oninput作爲onchange不會出現火每次一個鍵被按下時,僅在模糊

實施例1中添加的innerHTML元件

var currentInput = '1'; 
 
    //The input and function picker 
 
    document.getElementById('inputField').oninput = insertInput; 
 

 
    function insertInput() { 
 
     document.getElementById("inputHtml").innerHTML = currentInput; 
 
     console.log(currentInput); 
 
     var containsLaunchGame = /launch game/g; 
 
     var resultLaunchGame = currentInput.search(containsLaunchGame); 
 
     console.log(resultLaunchGame); 
 

 
     if (resultLaunchGame === 0) { 
 
     //scanGames(); 
 
     console.log("I found Launch Game"); 
 
     } 
 
    }
<div id="inputHtml"> 
 
    </div> 
 
    <input type="text" id="inputField" />

實施例2假設您想使用document.getElementById("inputText")而不是document.getElementById("inputHtml")

//The input and function picker 
 
    document.getElementById('inputField').oninput = insertInput; 
 

 
    function insertInput() { 
 
     currentInput = document.getElementById("inputField").value 
 
     console.log(currentInput); 
 
     var containsLaunchGame = /launch game/g; 
 
     var resultLaunchGame = currentInput.search(containsLaunchGame); 
 
     console.log(resultLaunchGame); 
 

 
     if (resultLaunchGame === 0) { 
 
     //scanGames(); 
 
     console.log("I found Launch Game"); 
 
     } 
 
    }
<input type="text" id="inputField" />

相關問題