I am simply trying to toggleClass on something based on if the form element is focused and if it's empty or not. But I can't figure out how to write the if then statement to test whether the field is empty or not.
Here is what I have:
$(document).ready(function() {
$('.step').focus(funct开发者_如何学运维ion() {
$(this).next("div").toggleClass("selected").fadeTo(500, 1);
});
$('.step').blur(function() {
if($(this[value==""])){
$(this).next("div").toggleClass("selected").fadeTo(250, 0.5);
}else{
return;
}
});
});
I have tried also if($(this).is(":empty"){
and it just kept acting like the field was empty even if it wasn't.
I also tried if($this).attr("value")==""){
How can I determine whether the field is empty?
Try $(this).val()
instead. That's jQuery's way of getting field values.
if($(this).val().length > 0) {
$(this).next("div").toggleClass("selected").fadeTo(250, 0.5);
} else {
return;
}
Assuming .step
is an input field, this should do:
if (this.value == "")
jQuery has the .val()
method that can be used to get the value of a form element. You could compare this to the empty string:
if($(this).val() !== "") {
// It has been filled
} else {
// It is empty
}
精彩评论