我意識到這可能是一個簡單的問題,但我是網絡開發的新手,對我來說很裸露。我正在嘗試通過html輸入框輸入用戶輸入值,並將其作爲我函數內的值(代碼中的negKeyword函數更具體)。我認爲發生的問題是,這個輸入值被存儲爲一個變量,所以當代碼首次存儲在內存中時,它被存儲爲「」,因爲用戶還沒有輸入任何東西。我如何得到它,以便當用戶輸入一些代替空白的東西或「」與用戶輸入的內容「將用戶輸入值插入函數
接下來我想要發生的事情是用戶將點擊一個按鈕,然後它會比較用戶輸入的內容和「negKeyword」函數輸出的內容並給出結果是否匹配(此操作已被演示在我的代碼中的booleanKeyword函數中)。
這是我的代碼。
var input = document.getElementById("input").value;
var arr = ['no', 'not', 'checked'];
var text = ''; //JS output variable.
var keyword = 'leak'; //Individual keyword.
function negKeyword() {
\t \t
for (i = 0; i < arr.length; i++) {
\t
\t if (text == input) { break; } \t
\t text = arr[i] + ' ' + keyword;
\t
\t }
\t return text;
}
\t
function booleanKeyword() { \t
\t if (input == negKeyword()) {
\t \t
\t \t document.getElementById("result").style.color="green";
\t \t document.getElementById("result").innerHTML="Match";
\t
\t } else {
\t
\t \t document.getElementById("result").style.color="red";
\t \t document.getElementById("result").innerHTML="No Match";
\t
\t }
\t
}
document.getElementById("result2").innerHTML=keyword;
<label for="Full Negative Keyword">Negative Keyword</label> <input id="input" type="text" />
<div id="message">Result: <span id="result"></span></div>
<div id="message">Keyword: <span id="result2"></span></div>
<button id="test" onclick="booleanKeyword()">Click to Test</button>
嗨,你想,當點擊時,它會重置輸入值? –
基本上可以,那是用戶輸入可以在函數中使用的。 –