0

我有這種形式使用,如果操作符&& ||在同等條件下

<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> 

這個腳本,以防止提交表單

$('.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; 
}  
}); 

我知道我的病情沒有工作,但我需要它這樣的工作

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

看看這個小提琴理解爲什麼http://jsfiddle.net/pK35e/

使用佔位符腳本i'm,需要我不提交表單上面 的情況下使用placeholder="attribute"對我來說是無解的,所以任何人都可以給我一個提示,該如何設置,如果條件?

+1

我不明白你爲什麼不代碼中使用括號。 – entonio 2013-03-05 17:41:07

+0

如果你被VB編碼風格寵壞了。 :) – codingbiz 2013-03-05 17:42:25

回答

2

試試這個

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

非常感謝你 – 2013-03-05 17:43:24

4

使用括號就像在你所做的文字描述:

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

邊備註:因爲它不是由IE9-支持您可能會感興趣,在將來的版本,由placeholder attribute這將使這更簡單。

+0

哇這些都是在30秒內最正確的答案,我曾經意識到加入到SO。謝謝我不知道那個作品很簡單 – 2013-03-05 17:43:07

+1

是的,我討厭佔位符屬性,因爲我不喜歡這個文本沒有隱藏在焦點上,並且每個瀏覽器都有它自己的渲染方式,這就是爲什麼我做了這個小小的佔位符腳本,即使在IE 6中也有我期望的外觀和感覺:D ...感謝您的信息鏈接 – 2013-03-05 17:47:05

+0

爲什麼不使用佔位符polyfill?那裏有很多,我無法想象爲什麼有人會推出自己的。我通常使用這一個:https://github.com/ginader/HTML5-placeholder-polyfill – Steve 2013-03-05 18:02:04

2
IF ((what == "what" ||what == "") &&(where == "where" ||where == "")) 
+0

也非常感謝你 – 2013-03-05 17:43:47

2

我相信你需要以一些括號得到你想要的東西:

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

這意味着,

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

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

必須返回如果你的政治家中的代碼是真的要執行。這實際上是非常接近你的文字的例子。

-

只是爲了理解這一切。您的舊代碼將看起來像這樣用括號:

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

又在哪裏,只是其中的一個人必須要返回true。

+1

謝謝你tooo – 2013-03-05 17:50:27