开发者

I want to check if `id` has no content

开发者 https://www.devze.com 2023-04-13 01:22 出处:网络
I wa开发者_开发技巧nt check between id that get in var span, if empty was between it put css for input but it not work. how can fix it?

I wa开发者_开发技巧nt check between id that get in var span, if empty was between it put css for input but it not work. how can fix it?

var span = '#'+$('.valid').closest('.auto_box').find('span').attr('id');
if ($(span+':empty').length != 0) {
    //alert('ok')
    (this).closest('.auto_box').find('input').css('background-color','#000');
}

See here my full code: http://jsfiddle.net/Pjqv2/2/


You are using (this) instead of $('.valid') or whatever you meant with it. Also, you are doing this the wrong way; .find('span') returns the jQuery objects set for that span.

You don't need to get it's ID and then check on that ID again. More importantly, your code seems the need to run on multiple instances of .auto_box. For that, you need to iterate on the set found by (".valid").closest(".auto_box"), which you can do with the jQuery .each() (.each() in jQuery docs) like this:

var autoBoxes = $(".valid").closest(".auto_box");
autoBoxes.each(function(){
    if ($(this).find("span").is(":empty")) {
        $(this).find("input").css("background-color", "#000");
    }
});

Your updated jsfiddle with this script: http://jsfiddle.net/dvir_azulay/Pjqv2/4/


Change (this) to $(span). I updated your fiddle to reflect this change.

0

精彩评论

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

关注公众号