开发者

Changing style of each empty element

开发者 https://www.devze.com 2023-04-05 04:23 出处:网络
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.

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');

0

精彩评论

暂无评论...
验证码 换一张
取 消