如果文本框内的长度小于 1,如何从文本框中隐藏清除图标。
我尝试了下面的代码,但它对我没有帮助。如果输入字段的长度小于 1,它不会隐藏内部图标。
示例演示是 点击这里
我尝试了下面的代码来隐藏它。
var myLength = $("#keywords").val().length;
if(myLength==0)
{
$("span").hide();// i tried to hide it but its not hiding
}
我的完整代码在这里
<!DOCTYPE html>
<html lang="en">
<head>
<title>SO question 2803532</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function() {
var myLength = $("#keywords").val().length;
if(myLength==0)
{
$("span").hide();// i tried to hide it but its not hiding
}
//show the innser clearable icon if users type any alphabets
$("input").keyup(function()
{
var textbox = $("#keywords").val().length;
if(textbox>=1)
$("span").hide();// i tried to hide it but its not hiding
});
$('input.deletable').wrap('<span class="deleteicon" />').after($('<span/>').click(function() {
$(this).prev('input').val('').focus();
}));
});
</script>
<style>
span.deleteicon {
position: relative;
}
span.deleteicon span {
position: absolute;
display: block;
top: 5px;
right: 0px;
width: 16px;
height: 16px;
background: url('http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=4') 0 -690px;
cursor: pointer;
}
span.deleteicon input {
padding-right: 16px;
}
</style>
</head>
<body>
<input type="text" class="deletable" id="keywords">
</body>
</html>