5

我试图将 CSS 添加到 keyup 的输入中。如果输入为空,则添加 CSS。我试过这个...

            $(input).keyup(function() {
                var input = $(this).attr('id');
                if( input == "" ) {
                  $(input).css( "border", "1px solid #FB9E25" );
                }   
            });
4

4 回答 4

7

http://jsfiddle.net/DKW7M/

HTML

<input type="text" id="test" />

JS:

$("#test").keyup(function() {
                var input = $(this);

                if( input.val() == "" ) {
                  input.css( "border", "1px solid #000" );
                }   
            });

我把它改成黑色以便更容易看到。

如果您想为多个 id 执行此操作,请尝试:

<form id="some_form">
 <input type="text" />
 <input type="text" />
</form>
$("#some_form input").each(function() {
     $(this).keyup(function() {

                var input = $(this);

                if( input.val() == "" ) {
                  input.css( "border", "1px solid #000" );
                }   
            });
   });
于 2013-05-14T07:48:54.730 回答
1

试试这个:

$('input').keyup(function() {
            var input = $(this).attr('id');
            if( input == "" || input == null) {
              $(this).css( "border", "1px solid #FB9E25" );
            }   
        });
于 2013-05-14T07:49:17.633 回答
1

如果您的意思是,如果没有值则要添加 css,请更改为:

$(input).keyup(function() {
     var input = $.trim( $(this).val() );
     if( input == "" ) {
         $(this).css( "border", "1px solid #FB9E25" );
     }   
});
于 2013-05-14T07:49:39.997 回答
0

尝试使用不同的名称:

$(input).keyup(function() {
    var inputId = $(this).attr('id');
    if( inputId == "" ) {
        $(input).css( "border", "1px solid #FB9E25" );
    }   
});
于 2013-05-14T07:49:12.840 回答