开发者

If statement in Javascript?

开发者 https://www.devze.com 2023-03-13 10:17 出处:网络
Is this code correct? if(!($(\'text开发者_高级运维area: name\').val().length == 0)) { alert(\"test\");

Is this code correct?

if(!($('text开发者_高级运维area: name').val().length == 0)) {
alert("test");
}

I want to check if there is something written or not inside the textarea field in the form? I ask because it's not working!?


You're missing your closing parens in your if statement. Try this:

 if(!( $('textarea: name').val().length == 0 ))
   {alert("test");}

There may be other jQuery selector issues.


if(!($('textarea').val().length == 0)) will work if you have only one textarea element in your page. I think what you were trying to do with that :name selector was select a specific textarea based on its name, in which case you need:

$('textarea[name=yourName]')


Since a length of 0 is "falsy", you can simplify your test to using just .length:

if ($('textarea[name=foo]').val().length) {
    alert(true);
} else {
    alert(false);
}

Here is a jsFiddle where you can play with it.


if ($('textarea: name').val().length > 0) {
    // do something if textbox has content
}
0

精彩评论

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