document.getelementbyid为输入类型返回undefined

时间:2016-02-04 20:19:57

标签: javascript html

我有一个简单的更改密码表单,其中一些脚本用于验证表单。问题是当调试我的表单时,id=password的输入字段返回undefined。我在哪里做错了?请提出任何建议。

  <?php
   if(isset($_POST['submit_pass']))
   {
    $password=mysql_real_escape_string($_POST['password']);
   $confirmpasssword=mysql_real_escape_string($_POST['confirmpassword']);
   if($password==$confirmpasssword)
   {
    $email=$_SESSION['login_email'];
   $insert=("update customers set password='$confirmpasssword' where email='$email'");
   $insert1=mysqli_query($con,$insert);
    echo "<script> alert('Your password is changed');</script>";
    }
   else
   {
    echo "<script> alert('Your password is not changed');</script>";
   }
    }
   ?>
   <script type="text/javascript">
    function showUser(str)
    {
     if (str=="")
      {
     document.getElementById("txtHint").innerHTML="";
      return;
     }
     if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
       xmlhttp=new XMLHttpRequest();
      }
       else
        {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.onreadystatechange=function()
         {
         if (xmlhttp.readyState==4 && xmlhttp.status==200)
          {
          document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
          }
           }
           xmlhttp.open("GET","check_password.php?q="+str,true);
           xmlhttp.send();
                }
            </script>

        <script type="text/javascript">
         function pass()
           {
       var cpwd=document.getElementById("confirmpassword").value;
       var pwd=document.getElementById("password").value;
      if(pwd==cpwd)
        {
        var t1=document.getElementById('pass').innerHTML="Password Match";
          }
         else
         {
         var t1=document.getElementById('pass').innerHTML="Password does not Match";
        document.form.password.focus();
         }
                }
            </script>



            <div id="password" class="tab-pane fade">

                <form id="form_setup" name="form_setup" method="post" action="" onSubmit="return passvali();">
                    <table width="50%" border="0" cellspacing="0" cellpadding="5" align="center" bgcolor="#C1C1FF">
                        <tr>
                            <td colspan="2" align="center"> <h2>Change Password</h2></td>
                        </tr>
                        <tr>
                            <th> Current Password <span style="color:#F00;">*</span></th>
                            <td><input type="password" name="cur_pwd" class="input" id="oldpassword"placeholder="Enter Current Password" onChange="showUser(this.value)" /></td>
                            <td> <span class="st" id="txtHint"></span> </td>
                        </tr>
                        <tr>
                            <th>New Password <span style="color:#F00;">*</span></th>
                            <td><input type="password" name="password" class="input" id="password" placeholder="Enter your Password"/></td>
                        </tr>
                        <tr>
                            <th> Confirm Password <span style="color:#F00;">*</span></th>
                            <td><input type="password" name="confirmpassword" class="input" id="confirmpassword" placeholder="Enter Confirm Password" onChange="return pass()" /></td>
                            <td><span id="pass" style="color:#F00;"> </span> </td>
                        </tr>
                        <tr>
                        </tr>
                        <tr>
                            <td colspan="2" align="center"><input type="submit" name="submit_pass" value="Submit" style="width:80px;" /></td>
                        </tr>
                    </table>
                </form>


            </div>

3 个答案:

答案 0 :(得分:1)

您有id="password"的两个元素:

enter image description here

ID必须是唯一的。

答案 1 :(得分:0)

您有2个名为“密码”的ID,请检查您的代码是否与ID不同

答案 2 :(得分:0)

首先有2个密码ID <div id="password" class="tab-pane fade">,第二个是输入字段,同一个名称id将在一个页面中一次。

使用focus()

更改document.forms[0].password.focus();功能代码