判断input框是否为空

需求:

  当input框失去焦点时:判断,如果输入框内容为空则获取焦点;否则弹出输入框的内容。

<!DOCTYPE html>
<html>
<head>
    <title>判断input是否为空</title>
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
    输入框:<input type="text" name="shurukuang" id="inputbox">
    <script type="text/javascript">
        // 输入框失去焦点时:判断  
        // 如果输入框内容为空则获取焦点;
        // 如果输入框内容不为空则弹出输入框的内容
        $(function () {
            $('#inputbox').blur(function(event) {
                if ($(this).val()=='') {
                    $(this).focus();
                }else{
                    alert($(this).val());
                }
            });
        })
    </script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/myunYao/p/8926153.html