1

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

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

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

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;
}

输入:

   <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:

$(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();
});
4

4 回答 4

7

为什么不使用类似的东西

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

在没有验证语法的情况下输入了这个。

于 2011-11-17T16:22:48.247 回答
0

替换

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

var show_label = function() { field.value || label.css( o.show_css )
于 2011-11-17T16:12:48.930 回答
0
$(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();
});
 });

});

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

于 2011-11-17T16:16:45.023 回答
0

所有这些编码都可以简化为:

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")

祝你好运!

于 2011-11-17T17:09:05.487 回答