2017-10-11 1 views
0

i nicht nur alle Eingänge einen roten Boden unter dem leeren Eingang setzen will, , was ich diese Arbeit zu machen, muß beheben oder in diesem Beispiel hinzufügen Wie jede Eingabe angeben

$(function() { 
 
    $('#submit').on('click', function() { 
 
    var myTitle = $('#title').val(), 
 
     myLocation = $('#location').val(), 
 
     myDescription = $('#description').val(), 
 
     myTime = $('#time').val(); 
 
    if (myTitle == '' || myDescription == '' || myLocation == '' || myTime == '') { 
 
     $('#title, #location, #description, #time').css('border-color', 'red'); 
 

 
     return false; 
 

 
    } 
 
    }); 
 
});
input { 
 
    border: none; 
 
    padding: 4px 0; 
 
    background: 0 0; 
 
    text-align: left; 
 
    outline: none; 
 
    display: block; 
 
    width: 100%; 
 
    border-bottom: 1px solid #ccc 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type='text' id='title' placeholder='Add title'> 
 
<input type='text' id='location' placeholder='Add location'> 
 
<input type='text' id='description' placeholder='Add description'> 
 
<input type='time' id='time'> 
 
<button id='submit'>submit</button>

Antwort

2

verwenden Sie einfach jede Funktion über die Ein- und prüfen, ob ist impty:

$(function() { 
 
    $('#submit').on('click', function() { 
 
     $("input").each(function() { 
 
      if ($(this).val() == "") { 
 
       $(this).css('border-color', 'red'); 
 
      } 
 
      else $(this).css('border-color', 'green'); 
 
     }); 
 
     return false; 
 
    }); 
 
});
input { 
 
    border: none; 
 
    padding: 4px 0; 
 
    background: 0 0; 
 
    text-align: left; 
 
    outline: none; 
 
    display: block; 
 
    width: 100%; 
 
    border-bottom: 1px solid #ccc 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type='text' id='title' placeholder='Add title'> 
 
<input type='text' id='location' placeholder='Add location'> 
 
<input type='text' id='description' placeholder='Add description'> 
 
<input type='time' id='time'> 
 
<button id='submit'>submit</button>

+0

Rhanks Liebe, ich weiß nicht, warum in einfachen Dingen mein Geist aufhört haha., Ich habe eine andere Frage, bitte, wenn Sie mir helfen können, Ich habe Eingabe Typ Suche., Ich kann es deaktivieren durch >> prop. ('disabled', true) meine frage ist wie man das umschalten kann, ich probierte prop. ('disabled', false) und removeAttr, und beide funktionen funktionieren nicht, hast du eine idee –

+0

In welchem ​​fall willst du toggle disabled Eingabesuche? – SilverSurfer

+0

https://stackoverflow.com/questions/46712794/how-i-can-toggle-the-input-statue-disabled –

Verwandte Themen