使用 jQuery 如果文本输入不为空则显示元素
好的,所以我对元素进行了编码等等。这有点奇怪,因为如果表单为空,则会以图像的形式显示一个清晰的按钮,例如:(x)
这是脚本,在页面加载时 jQuery 聚焦于文本输入字段,同时显示(十)明确班级。我想要达到的目的就是这个。
页面加载时,jQuery 仍然关注文本输入字段,但它不会显示清除类 (x),但只有在文本字段中输入至少 1 个字符时才会显示它,否则不会显示。
这是当前的代码
$(document).ready(function() {
$('#ui_query').focus();
});
$('#ui_query').focus(function() {
$('.clear-helper').css('opacity','0.9999');
});
$(document).focusout(function() {
$('.clear-helper').css('opacity','0.3333');
});
(function ($, undefined) {
$.fn.clearable = function () {
var $this = this;
$this.wrap('<div class="clear-holder" />');
var helper = $('<span class="clear-helper"></span>');
$this.parent().append(helper);
helper.click(function(){
$this.val("");
$('#ui_query').focus();
});
};
})(jQuery);
$("#ui_query").clearable();
Ok, so I got the element coded and such. It just bit odd, since if the form is empty a clear button is showing in a form of an image ex: (x)
Here is the script, upon page load jQuery focuses on the text input field, and in the same time displays the (x) clear class. What I wish to achieve is this.
Upon page load jQuery still focuses on the text input field, however it does not display clear class (x) but will display it only if there is at least 1 character inputed into the text field other vise it is not displayed.
here is the current code
$(document).ready(function() {
$('#ui_query').focus();
});
$('#ui_query').focus(function() {
$('.clear-helper').css('opacity','0.9999');
});
$(document).focusout(function() {
$('.clear-helper').css('opacity','0.3333');
});
(function ($, undefined) {
$.fn.clearable = function () {
var $this = this;
$this.wrap('<div class="clear-holder" />');
var helper = $('<span class="clear-helper"></span>');
$this.parent().append(helper);
helper.click(function(){
$this.val("");
$('#ui_query').focus();
});
};
})(jQuery);
$("#ui_query").clearable();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 attribute-not-equals 属性选择器来定位具有非-空白值:
如果您不希望空格字符被视为“某物”,那么您可以执行以下操作:
You can use the attribute-not-equals attribute selector to target inputs with non-blank values:
If you don't want a space character to count as "something" then you can do: