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 ?
- 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
5 Answers
Reset to default 4Uses 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条)