Changing style of each empty element
I am working on a project where a user can add items i have provided them a "add another" button so that they can add another item so now they can add more then one items on the same time. To validate this form and to insert values in db I have used the following procedure
Now I am accessing the开发者_如何学C form elements through its name but my client wants to change the background color of each element the is left blank by the user. I have tried Jquery "empty", "nth child" selectors but i am totally failed to do this so please help me to solve this problem
Are you only looking to change the background color of empty text boxes? If so, you could do:
$("#my_form input:text").each(function() {
if(!$(this).val()) {
$(this).css("background-color", "red");
}
});
You iterate over all the elements you want validated in #my_form and check to see if they have any values. If not, apply your CSS changes.
Use selectors $('#form input:text[value=""]').css('background-color','red');
精彩评论