Javascript:禁用文字字段无法正常工作

时间:2017-03-08 07:04:27

标签: javascript

我已经尝试编写一些javascript的表单,当用户输入某个单词时,下面的文本字段将自动禁用。问题是,当我尝试jsfiddle中的代码时,代码将运行顺利,但当我在我的使用它项目,它没有工作。 任何人都可以解释我的代码有什么问题。

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

6 个答案:

答案 0 :(得分:2)

问题1:错字char* s = "MIDSH"; printf("%d\n", strlen(s)); //print 5 s++; printf("%d\n", strlen(s)); //print 4 ,将其替换为matchword()

问题2:修剪是一种方法!请改用matchWord();

&#13;
&#13;
document.getElementById('uname').value.trim()
&#13;
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');
  }
}
&#13;
&#13;
&#13;

答案 1 :(得分:2)

首先 trim()不是属性,它是一种方法。因此,您需要将其从trim更改为trim()

第二次您需要为onkeyup分配一个函数,而不是调用函数。

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

答案 2 :(得分:1)

您将KeyUp事件的eventhanlder设置为函数的结果,并且您想要设置函数本身。

尝试:

document.getElementById('uname').onkeyup = matchWord;

相反,或者在元素本身中添加事件处理程序,删除上一行并添加:

<input type="text" name="uname" id="uname" placeholder="Username" onkeyup="matchWord()">

答案 3 :(得分:1)

您提供的示例中有一些错误。

  • 首先,名称区分大小写,因此matchWordmatchword不同。小心!
  • 其次,您需要使用document.getElementById('uname').onkeyup = matchWord;将函数本身分配给事件,而不是它返回的结果。
  • 第三点也是最后一点,您的方法中的逻辑是错误的,因为您使用trim而不是trim(),这是上面提到的错误的完全相反的错误,这将返回函数而不是价值。以下代码段有效:

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

答案 4 :(得分:0)

Below code works fine for me in all browsers

<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>
   <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').addEventListener("keyup", function(evt) {
        matchWord(evt);
    }, false);
</script>
</html>

答案 5 :(得分:0)

把:

<script> // stuff </script>

在:

<html> // here </script>
相关问题