开发者

Need help figuring out how to exclude selecting an element if it has text in it

开发者_Python百科I would like to select and element with a class called "validate_me", but I don't want to include any elements if they have text already inside them, but I'm stuck on the jquery. It looks like ":contains()" could do the trick but I don't want to search for a particular string, I just want to "exclude" any element that already has text in it.

I hope this makes sense...How can I do this?

Thanks in advance.

Here's what I attempted before, but it obviously doesn't work:

$("input.validate_me:not(:contains('any text is allowed')),select").addClass("ui-state-error"); 


$('input.validate_me').filter(function() { return $(this).val() == ""; }).addClass("ui-state-error");


Try this

Wroking demo

$(function(){
    $("input.validate_me").filter(function(){
        return !$(this).val();
    }).add("select").addClass("ui-state-error"); 
});


$('input').each(function() {

    if ($(this).val() != '') {

        $(this).addClass("ui-state-error");
    }
});

http://jsfiddle.net/Anank/


and for the sake of more than one solution, you can always put your undesired nodes in an array and pass that to the .not() method

http://jsfiddle.net/SNXzU/

var nodeArray = [];
var nodeList = $("input.validate_me");

for (var i = 0; i < nodeList.length; i++ )
    if ($("input.validate_me")[i].value.length > 0)
        nodeArray.push($("input.validate_me")[i]);

nodeList.not(nodeArray).addClass("ui-state-error");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜