开发者

or operator with jquery not working how i'd expect..?

开发者 https://www.devze.com 2023-03-07 18:08 出处:网络
$(\'input[TYPE=\"SUBMIT\"]\').click(function(){ if ($(\'input[TYPE=\"TEXT\"\').val().length===0 || $(\'textarea\').val().length===0)
$('input[TYPE="SUBMIT"]').click(function(){
    if ($('input[TYPE="TEXT"').val().length===0 || $('textarea').val().length===0)
    {
        console.log($('input[TYPE="TEXT"').val().length);
        console.log($('textarea').val().length);
        return false;
    }
});

chrome highlights my if statement. I really don't see what's wrong here. I suppose I could add a class to both fields, 开发者_运维技巧but I don't really need that for my layout. Am I writing this incorrectly somehow?


What I've immediately noticed is that

$('input[TYPE="TEXT"')

should be

$('input[TYPE="TEXT"]')

Note, however, that making a selection like this will only check the length of the first textbox/textarea it finds, not each of them.

What you'd need in that case would be something like this:

$("input[type='submit']").click(function()
{
    var ok = true;
    $("input[type='text'], textarea").each(function() {
        ok &= ok && $(this).val().length > 0;
    });
    alert(ok ? 'All OK' : 'Not OK :(');
});

Even better would be giving the submit button a proper ID and the input elements some other attribute, so you don't accidentally include other controls in the validation.

<input type="text" validate="true" />

$("*[validate='true']").each(function() {
    ok &= ok && $(this).val().length > 0;
});


($('input[TYPE=TEXT]').val().length == 0 || $('textarea').val().length == 0)


It looks like you have 'input[TYPE="TEXT"' with a missing ] in quite a few places, could that be the problem?


$('input[TYPE="TEXT"/*]*/')

You forgot the ]

I recommend you use the :text pseudo selector instead ;)

$("input:text");

:text

0

精彩评论

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