验证输入框,使用Jquery focusin函数不起作用

时间:2014-11-14 21:32:47

标签: javascript jquery html focus focusin

我正在为在网站上创建新帐户的人们制作验证表单。当用户进入第二个输入框时,如果第一个输入框(用户名)为空或者数据库中已存在相同的用户名,则会收到错误消息。 如何在开始输入第二个输入框时显示错误消息?我使用了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>

1 个答案:

答案 0 :(得分:0)

由于

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

没有检查该值,您正在将jQuery对象与空字符串进行比较。

$("input[name='first_name']").val()