如果使用jQuery输入的文本不为空,则显示一个元素

| 好的,所以我将元素编码为此类。有点奇怪,因为如果表单为空,则会以图像形式显示清除按钮,例如:(x) 这是脚本,页面加载时jQuery专注于文本输入字段,同时显示(x)清除类。我希望实现的是这一点。 在页面加载时,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();
    
已邀请:
您可以使用attribute-not-equals属性选择器来定位具有非空白值的输入:
$(\'#ui_query[value!=\"\"]\').doSomething();
如果您不希望空格字符算作“某物”,则可以执行以下操作:
if($.trim($(\'#ui_query\').val()) !== \"\") {
    $(\'.clear-helper\').css(\'opacity\',\'0.9999\');
}
    

要回复问题请先登录注册