javascript - Using if operators && || in the same condition - Stack Overflow

I have this form <form class="form" method="post"><input type="text&

I have this form

<form class="form" method="post">
<input type="text" id="input_what" holder="what" />
<input type="text" id="input_where" holder="where" />
<input type="submit" value="submit" />
</form>

and this script to prevent submitting the form

$('.form').submit(function(e) {
var what = $('#input_what').val();
var where = $('#input_where').val()
if ( what == "what" || what ==""  &&  where == "where" || where == "") {
   e.preventDefault();
   console.log('prevented empty search');
   return false;
}     
});

I know that my condition doesn't work, but i need it to work like this

 IF (what == "what" OR what == "") AND (where == "where" OR where == "")

have a look at this fiddle to understand why /

the placeholder-script i´m using, needs me to not submit the form for the cases above using placeholder="attribute" is no solution for me, so can anyone give me a hint how to set this if-condition ?

I have this form

<form class="form" method="post">
<input type="text" id="input_what" holder="what" />
<input type="text" id="input_where" holder="where" />
<input type="submit" value="submit" />
</form>

and this script to prevent submitting the form

$('.form').submit(function(e) {
var what = $('#input_what').val();
var where = $('#input_where').val()
if ( what == "what" || what ==""  &&  where == "where" || where == "") {
   e.preventDefault();
   console.log('prevented empty search');
   return false;
}     
});

I know that my condition doesn't work, but i need it to work like this

 IF (what == "what" OR what == "") AND (where == "where" OR where == "")

have a look at this fiddle to understand why http://jsfiddle/pK35e/

the placeholder-script i´m using, needs me to not submit the form for the cases above using placeholder="attribute" is no solution for me, so can anyone give me a hint how to set this if-condition ?

Share Improve this question edited Mar 5, 2013 at 17:50 Ben McCormick 25.7k12 gold badges55 silver badges71 bronze badges asked Mar 5, 2013 at 17:38 john Smithjohn Smith 17.9k12 gold badges78 silver badges122 bronze badges 2
  • 1 I didn't understand why you don't use parens in your code. – entonio Commented Mar 5, 2013 at 17:41
  • If you were spoilt by VB coding style. :) – codingbiz Commented Mar 5, 2013 at 17:42
Add a ment  | 

5 Answers 5

Reset to default 4

Uses parenthesis just like in the textual description you made :

if (( what == "what" || what =="")  &&  (where == "where" || where == "")) {

Side remark : You might be interested, for future versions as it's not supported by IE9-, by the placeholder attribute which will make this simpler.

Try this

if ( (what == "what" || what =="")  &&  (where == "where" || where == ""))
IF ((what == "what" ||what == "") &&(where == "where" ||where == ""))

Use parens. The && operator has a higher precedence than the || operator.

if ((what == "what" || what =="") && (where == "where" || where == ""))

http://en.m.wikipedia/wiki/Order_of_operations

I believe you need some parenthesis in order to get what you want:

if ( (what == "what" || what =="")  &&  (where == "where" || where == ""))

This means that both

(what == "what" || what =="") 

and

(where == "where" || where == "") 

has to return true in order for the code within your if statement to be executed. This is actually quite close to your textual example.

--

Just for the understanding of all of this. Your old code would look like this with parenthesis:

if ( (what == "what") || (what ==""  &&  where == "where") || (where == "")) {

Where again, just one of these would have to return true.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744724418a4590098.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信