需求分析
我想要判断输入框的值是否为空,如果为空就弹框提示,并且返回焦距到输入框。
实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<input type="text" id="inputValue" placeholder="请输入内容" />
<input type="button" value="提交" id="btn" />
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$('#btn').click(function () {
if ($('#inputValue').val() == '' || $.trim($('#inputValue').val() == '')) {
$('#inputValue').focus()
$('#inputValue').attr('placeholder', '内容不能为空')
}
})
</script>
</html>
评论 (0)