如果在.keyup上输入为空,则Jquery添加CSS

afr*_*360 5 jquery

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

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

小智 7

http://jsfiddle.net/DKW7M/

HTML

<input type="text" id="test" />
Run Code Online (Sandbox Code Playgroud)

JS:

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

                if( input.val() == "" ) {
                  input.css( "border", "1px solid #000" );
                }   
            });
Run Code Online (Sandbox Code Playgroud)

我将它改为黑色,以便更容易看到.

如果你想为多个id做,请尝试:

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

                var input = $(this);

                if( input.val() == "" ) {
                  input.css( "border", "1px solid #000" );
                }   
            });
   });
Run Code Online (Sandbox Code Playgroud)