2017-08-28 112 views
-3

我有這個JS變種,其次是這個if語句:if語句問題進入

if ($('custom_t4')) 
       var contratType = $('custom_t4').value === 'Nouvelle_affaire' ? 'Nouvelle Affaire' : $('custom_t4').value === 'Avenant' ? 'Avenant' : ''; 



if (contratType && (contratType !== 'Nouvelle Affaire' || contratType !== 'Avenant')){ } 

我的問題是,當contratType定義,他的價值是「中篇小說AFFAIRE」,仍然是輸入如果。

我錯過了什麼?

+1

*「如果foo是不是‘欄’或不是‘巴茲’」 * - 它不能同時既,所以它永遠*不*是一個或另一個。換句話說,那個條件總是*真實的。 – deceze

+1

除了'contratType!=='Nouvelle Affaire'|| contratType!=='Avenant''永遠是真的(我標記爲重複的部分),你也遇到了[這個問題(jQuery對象總是真的)](https://stackoverflow.com/questions/ 31044/is-there-an-exists-function-for-jquery)**和**錯誤地獲取元素的值(jQuery對象沒有'value'屬性;你可以得到一個'input'的值一個jQuery對象,通過['val'](http://api.jquery.com/val/))。 –

回答

1

在第二個條件OR(||)運算符是問題。正確的代碼下面寫

if (contratType && (contratType !== 'Nouvelle Affaire' && contratType !== 'Avenant')){ }