如果文本框为空,则将其返回到默认值

Return a textbox to defaultvalue if the textbox is empty

本文关键字:返回 默认值 文本 如果      更新时间:2023-09-26

我有一个用于搜索的文本框。 由于某种原因,.blur() 在我的计算机上不起作用。我创建了一个测试文件来测试它,但它仍然不起作用:

<!doctype html>
<html>
 <head>
<meta charset="utf-8">
<title>Demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
    $('#search').blur(function() {
        alert('hello');
});
</script>
</head>
<body>
<input id="search" value="hello" type="text" />
</body>
</html>

就像戴夫说的,试试这个:

$(document).ready(function() {
    $('#search').blur(function() {
        alert('hello');
    });
});

也许只是使用非jquery的方式来做事?

document.getElementById("search").addEventListener('blur',function(){
    document.getElementById("search").value = "DEFAULT_VALUE";
});