javascript 如果输入包含值,Jquery 隐藏标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8170367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:31:34  来源:igfitidea点击:

Jquery hide label if input contains value

javascriptjqueryonfocus

提问by hairynuggets

I am using the following code to hide and show a label in my form with Jquerys onBlur and Focus.

我正在使用以下代码通过 Jquerys onBlur 和 Focus 在我的表单中隐藏和显示标签。

Everything works apart from the label showing when there is a value in one of the inputs. (I echo values into my inputs using php when validation fails so users don't have to retype)

除了显示输入之一中何时存在值的标签之外,一切都有效。(当验证失败时,我使用 php 将值回显到我的输入中,因此用户不必重新输入)

How can I get the following code to check if the input has a value and if so hide the label and show it again when value is blank:

如何获取以下代码以检查输入是否具有值,如果是,则隐藏标签并在值为空时再次显示它:

CSS

CSS

div#first-name,
div#last-name,
div#email-address,
div#password
 {
    position:relative;
    float:left;
    margin-right:3px;
}
label.overlabel {
    color:#999;
}
label.overlabel-apply {
    position:absolute;
    top:3px;
    left:5px;
    z-index:1;
    color:#999;
    padding-left:10px;
    padding-top:10px;
}

Input:

输入:

   <div id="first-name">
 <label for="first-name-field" class="overlabel">First name</label>
 <input id="first-name-field" type="text" name="first_name" />
   </div>

Javascript:

Javascript:

$(document).ready(function() {



    // plugin definition
    $.fn.overlabel = function( options ) {

        // build main options before element iteration
        var opts = $.extend( {}, $.fn.overlabel.defaults, options );

        var selection = this.filter( 'label[for]' ).map( function() {

            var label = $( this );
            var id = label.attr( 'for' );
            var field = document.getElementById( id );

            if ( !field ) return;

            // build element specific options
            var o = $.meta ? $.extend( {}, opts, label.data() ) : opts;

            label.addClass( o.label_class );

            var hide_label = function() { label.css( o.hide_css ) };
            var show_label = function() { this.value || label.css( o.show_css ) };

            $( field ).
                 parent().addClass( o.wrapper_class ).end().
                 focus( hide_label ).blur( show_label ).each( show_label );

        //How to hide label if input contains value

            return this;

        } );

        return opts.filter ? selection : selection.end();
    };

    // publicly accessible defaults
    $.fn.overlabel.defaults = {

        label_class:   'overlabel-apply',
        wrapper_class: 'overlabel-wrapper',
        hide_css:      { 'text-indent': '-10000px' },
        show_css:      { 'text-indent': '0px', 'cursor': 'text' },
        filter:        false

    };

$(document).ready(function() {
    $("label.overlabel").overlabel();
});

回答by AyKarsi

why not use something like

为什么不使用类似的东西

if ($(field).val() != ""){
  var fieldId = $(field).attr("id");
  $("label[for='"+fieldId+"']").hide();
}

typed this without validating syntax.

在没有验证语法的情况下键入此内容。

回答by roselan

repalce

替换

var show_label = function() { this.value || label.css( o.show_css )

with

var show_label = function() { field.value || label.css( o.show_css )

回答by Senad Me?kin

$(document).ready(function(){
   $('#form-id input').each(function() { if($(this).val().length > 0) $('#form-id label[for="' + $(this).attr('id') + '"]').hide();
$(this).change(function() {
   if($(this).val().length > 0)
       $('#form-id label[for="' + $(this).attr('id') + '"]').hide();
   else 
       $('#form-id label[for="' + $(this).attr('id') + '"]').show();
});
 });

});

this could help you, I have searched through input values on load and if there is value hide label related to that input, and also I have attached event "onChange" if there is value on blur then hide label.

这可以帮助你,我已经搜索了加载时的输入值,如果有与该输入相关的值隐藏标签,我还附加了事件“onChange”,如果有关于模糊的值,然后隐藏标签。

回答by zequinha-bsb

All that coding could just have be reduced to this:

所有的编码都可以简化为:

jQuery('#form-id input').blur( function () {
    if ($(this).val() == '') 
        $(this).parents("div").find("label[for=" + this.id + "]").show().text();
}).focus( function () {
    $(this).parents("div").find("label[for=" + this.id + "]").hide().text();
});

==> using this approach, make sure that label is 
    hard coded with "overlabel-apply"

==> also, to have a whole form scope (in case you are not putting each
    input in a div), change $(this).parents("div") to $(this).parents("#form-id")

Good luck!

祝你好运!