我試着編寫一些JavaScript的形式,當用戶輸入某個單詞,然後下面的textfield會自動disable.The問題是,當我嘗試在jsfiddle代碼將運行順利,但是當我在我的項目中使用它時,它不起作用。 任何人都可以解釋我的代碼有什麼問題。Javascript:禁用textfield不起作用
<script>
function matchWord(e){
if(document.getElementById('uname').value.trim === 'Unknown' || document.getElementById('uname').value.trim === 'unknown'){
document.getElementById('pword').disabled = true;
document.getElementById('response').disabled = true;
alert('you enter bannned name in the field');
}
else{
}
}
document.getElementById('uname').onkeyup = matchWord();
</script>
<html>
<body>
Name:<input type="text" name="uname" id="uname" placeholder="Username"><br>
password:<input type="password" name='pword' id='pword' placeholder="password"><br>
Responsibility:<select id = "response">
<option>---Responsibility---</option>
</select>
</body>
</html>
錯字:'函數matchWord'(駝峯)VS'=速配關鍵字'(小寫)。 –
只是修復它,但仍然無法正常工作。 – Nexz
您是否檢查過瀏覽器的devtools錯誤?一個可能的解釋是「[爲什麼jQuery或諸如getElementById之類的DOM方法未找到該元素?](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method - as-getelementbyid-not-the-element)「 –