无法读取表单中未定义错误的属性“值”

时间:2016-11-09 01:31:30

标签: javascript forms

我使用的是基于表单模板here的注册表单。它工作得很好,但我还需要它添加名字和姓氏以及电话号码,所以我把它改成了这个:

        <form method="post" name="registration_form" action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>">
            First Name: <input type='text' name='firstname' id='firstname' /><br>
            Last Name: <input type='text' name='lastname' id='lastname' /><br>
            Phone: <input type='tel' name='phone' id='phone' /><br>
            Username: <input type='text' name='username' id='username' /><br>
            Email: <input type="text" name="email" id="email" /><br>
            Password: <input type="password"
                             name="password" 
                             id="password"/><br>
            Confirm password: <input type="password" 
                                     name="confirmpwd" 
                                     id="confirmpwd" /><br>
            <input type="button" 
                   value="Register" 
                   onclick="return regformhash(this.form,
                                   this.form.firstname,
                                   this.form.lastname,
                                   this.form.phone,
                                   this.form.username,
                                   this.form.email,
                                   this.form.password,
                                   this.form.confirmpwd);" /> 
        </form>

我还必须调整Javascript表单以接受新值,现在就是这样:

function regformhash(form, uid, firstname, lastname, phone, username, email, password, confirmpwd) {
    // Check each field has a value
    if (uid.value == '' || firstname.value == '' || lastname.value == '' || phone.value == '' || email.value == '' || password.value == '' || confirmpwd.value == '') {
        alert('You must provide all the requested details. Please try again');
        return false;
    }

我现在在“表单”页面和与“confirmmpwd”字段关联的Javascript页面上收到“无法读取未定义的属性”值“错误”。该字段不为空,并且名称未从原始模板更改,唯一改变的是首字母和姓氏和电话已添加到表单中。

2 个答案:

答案 0 :(得分:0)

两件事,你的功能不匹配。首先,您传递的是8个参数,不包括uid的值到期望9个输入的函数。这些值最终不会按照您的预期排列,因此,当函数运行时,confirmpwd未定义,并且当您尝试获取未定义的属性时,会出现"Cannot read property 'value' of undefined"错误

由于uid不在表单中,而您也未验证username,我继续并假设您正在尝试验证username并相应更新您的功能。

function regformhash(form, firstname, lastname, phone, username, email, password, confirmpwd) {
    // Check each field has a value
    if (username.value == '' || firstname.value == '' || lastname.value == '' || phone.value == '' || email.value == '' || password.value == '' || confirmpwd.value == '') {
        alert('You must provide all the requested details. Please try again');
    }
    return false;
}
<form method="post" name="registration_form" action="<?php echo esc_url($_SERVER['PHP_SELF']); ?>">
    First Name: <input type='text' name='firstname' id='firstname' /><br>
    Last Name: <input type='text' name='lastname' id='lastname' /><br>
    Phone: <input type='tel' name='phone' id='phone' /><br>
    Username: <input type='text' name='username' id='username' /><br>
    Email: <input type="text" name="email" id="email" /><br>
    Password: <input type="password"
                        name="password" 
                        id="password"/><br>
    Confirm password: <input type="password" 
                                name="confirmpwd" 
                                id="confirmpwd" /><br>
    <input type="button" 
            value="Register" 
            onclick="return regformhash(this.form,
                            this.form.firstname,
                            this.form.lastname,
                            this.form.phone,
                            this.form.username,
                            this.form.email,
                            this.form.password,
                            this.form.confirmpwd);" /> 
</form>

答案 1 :(得分:-1)

你忘记了uid参数