字段在javascript中被识别为空

时间:2018-05-21 17:09:20

标签: javascript

问题是输入中输入的文本未被识别,并且给我一个空值。

function checkInput(name, surname, email, comment) {
    if (name !==null &&  name!== '') {
        if (surname !==null &&  surname!== '') {
            if (email.value == '') {
                if (validateEmail(email)) {
                    if (comment !==null &&  comment!== '') {
                        window.alert('Valid inputs')
                    } else {
                        window.alert('You have not entered in a comment')
                    }
                } else {
                    window.alert('Email is not valid.')
                }
            } else {
                window.alert('You have not entered an email')
            }
        } else {
            window.alert('Please enter a surname')
        }
    } else {
        window.alert('Please enter a name')
    }
}

我也试过这样做但仍然没有。似乎问题来自if语句,但我不确定为什么它不能正常工作

function checkInput(name, surname, email, comment) {
    if (name.value == '') {
        if (surname.value == '') {
            if (email.value == '') {
                if (validateEmail(email)) {
                    if (comment.value == '') {
                        window.alert('Valid inputs')
                    } else {
                        window.alert('You have not entered in a comment')
                    }
                } else {
                    window.alert('Email is not valid.')
                }
            } else {
                window.alert('You have no entered an email')
            }
        } else {
            window.alert('You have not entered a surname')
        }
    } else {
        window.alert('You have not entered a name')
    }
}

1 个答案:

答案 0 :(得分:0)

我有一个使用js的答案,但这个答案更简单,效果更快

<select required>
    <option value="" selected disabled hidden></option>
    <option></option>
</select>

如果将所需的属性添加到输入或选择,当提交表单但输入中没有数据或选择时,它会创建一个标题,指出输入是必需的。

?- write('Hello World'),nl.