0

我正在为在网站上创建新帐户的人制作验证表格。当用户输入第二个输入框时,如果第一个输入框(用户名)为空或数据库中已有相同的用户名,他们将收到错误消息。当他们开始输入第二个输入框时如何显示错误消息?我用了focusin,但没有用。你可以帮帮我吗??

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("input[name='last_name']")    .focusin(function(){
    if($("input[name='first_name']")=="")
    {
        $("#firstname_error").html("Please enter your firstname")
        }

    })
})
</script>
</head>
<body>
<input type="text" name="first_name" placeholder="First Name" onFocus="this.placeholder=''" onBlur="this.placeholder='First Name'" />
                    <div id="firstname_error" class="error_message hide_max_desktop"></div> <!-- end firstname_error -->
                    <input type="text" name="last_name" placeholder="Last Name" onFocus="this.placeholder=''" onBlur="this.placeholder='Last Name'" />
                    <div id="lastname_error" class="error_message hide_max_desktop"></div>

</body>
</html>
4

1 回答 1

0

因为

if($("input[name='first_name']")=="")

不是检查值,而是将 jQuery 对象与空白字符串进行比较。

$("input[name='first_name']").val()
于 2014-11-14T21:37:19.553 回答