If statement in Javascript?
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
}
精彩评论