0

我对 js 几乎不陌生,但对 php-html 网络编程并不陌生,但我对 js 的使用感到困惑。
我正在使用这个 js 函数来比较两个输入(密码检查),
这是小提琴
http://jsfiddle.net/EHUWC/1/

那里什么都没有,对吧?
但它不适用于我自己的网页,
这是我的代码(我使用 codeigniter):

 <div class="centerTitle">Sign Up Form</div>
 <script>
  function chkpassword() {
   var p1 = document.getElementById("pass1").value;
   var p2 = document.getElementById("pass2").value;
   if (p1.length > 5) {
    document.getElementById("passwordAlert").style.display = 'none';
    if (p1 === p2) {
     document.getElementById("passwordAlert").style.display = 'none';
     validpass = "yes";
    } else {
      validpass = "no";
      document.getElementById("passwordAlert").style.display = 'block';
      document.getElementById("passwordAlert").innerHTML = ":( Both passwords must match.";
    }
   } else {
    document.getElementById("passwordAlert").style.display = 'block';
    document.getElementById("passwordAlert").innerHTML = ":( The password must be at least 6 characters long.";
   }
 }
 </script>
 <?php $attributes = array('name' => 'Form1','onsubmit' => 'return validate()');
     echo form_open("visitor/confirm", $attributes);?>
 <table>
 <tr class='signup'>
  <td class='signup1'>Password*</td>
  <td class='signup2'>:</td>
  <td class='signup3'>
   <input id="pass1" onkeyup="chkpassword()" class="signup" type="password" value="" maxlength="20" name="password" />
  </td>
 </tr>
<tr class='signup'>
    <td class='signup1'>Confirm Password*</td>
    <td class='signup2'>:</td>
    <td class='signup3'>
        <input id="pass2" onkeyup="chkpassword()" class="signup" type="password" value="" maxlength="20" name="chkpassword" />
        <div id="passwordAlert"></div>
    </td>
</tr>
</table>
<input type="submit" value="Daftar">
<?php echo form_close();?>

(我没有向这个视图传递任何内容)
如果我在密码输入时出错,那么 div 将不会出现
,如果我检查元素并查看 chrome 上的控制台,它说 我错过了onkeyupgetUncaught TypeError: Object is not a function错误
?
为什么它在 jsfiddle 上运行时没问题?

4

1 回答 1

0

我更改了确认密码名称(chkpassword),因为它与 js 函数的名称相同。

<input id="pass2" onkeyup="chkpassword()" class="signup" type="password" value="" maxlength="20" name="chkpassword" />
<input id="pass2" onkeyup="chkpassword()" class="signup" type="password" value="" maxlength="20" name="chkpasswordx" />

感谢@Sumurai8

于 2013-06-26T12:04:09.383 回答