2016-07-24 78 views
0

我意識到這可能是一個簡單的問題,但我是網絡開發的新手,對我來說很裸露。我正在嘗試通過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>

+0

嗨,你想,當點擊時,它會重置輸入值? –

+0

基本上可以,那是用戶輸入可以在函數中使用的。 –

回答

0

你可以得到它,並分配給同一個變量(但就是按一下按鈕後調用的函數內)再次檢索輸入的值。

var input = document.getElementById("input").value; 
 
var arr = ['no', 'not', 'checked']; 
 
var text = ''; //JS output variable. 
 
var keyword = 'leak'; //Individual keyword. 
 

 
function negKeyword() { 
 

 
    input = document.getElementById("input").value; 
 
\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 
 

 
     input = document.getElementById("input").value;//The variable is reassigned, only after the click 
 

 
\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;

編輯:添加了相同的代碼來negKeyword()功能,因爲它需要的輸入了。

+0

這工作,謝謝! –

+0

儘管不是最好的解決方案,但應該在事件偵聽器中使用'this'對象,並將其值傳遞給需要它的任何函數(作爲參數),而不是重新讀取DOM中的輸入值。 – trincot

+0

的確,我只是想盡可能少地改變代碼。 – yuriy636

0

它不工作,因爲你的變量輸入總是「」。每次單擊按鈕時都必須爲其分配新值。我只是將您的代碼移動到BooleanKeyword()函數中進行輸入。現在一切正常。

每當這樣的事情不能正常工作時,只要嘗試記錄/提醒值即可。例如,你可以只提醒(輸入+''+ negKeyword()); booleanKeyword()函數的頂部,你會看到自己的問題。

var input; 
 
var arr = ['no', 'not', 'checked']; 
 
var text = ''; //JS output variable. 
 
var keyword = 'leak'; //Individual keyword. 
 

 
function negKeyword() { 
 
     
 
for (i = 0; i < arr.length; i++) { 
 
    
 
    if (text == input) { break; } 
 
    text = arr[i] + ' ' + keyword; 
 
    
 
    } 
 
    return text; 
 
} 
 
    
 
function booleanKeyword() { 
 
    input = document.getElementById("input").value; 
 
    if (input == negKeyword()) { 
 
     
 
     document.getElementById("result").style.color="green"; 
 
     document.getElementById("result").innerHTML="Match"; 
 
    
 
    } else { 
 
    
 
     document.getElementById("result").style.color="red"; 
 
     document.getElementById("result").innerHTML="No Match"; 
 
    
 
    } 
 
    
 
} 
 

 
document.getElementById("result2").innerHTML=keyword;
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title></title> 
 
</head> 
 
<body> 
 
<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> 
 

 
</html>