2013-02-28 72 views
1

我正在嘗試填寫表單中的一些字段。我正在做一個測試,如果extnReasonextnDtnull,我什麼都不做。但由於某種原因,它不斷進入檢查和加載我的領域與null,我不想。Javascript在JSP中輸入測試空值

function preloadFields() { 

//these values are coming in as null 
var extnReason = '<%=extnReason%>'; 
var extnDt = '<%=extnDt%>'; 

//set Extension Date blank on load 
$('#extnDt').val(""); 

alert("reason ++++ " + extnReason); 
alert("extnDt ++++ " + extnDt); 

//it is entering these tests but I don't want them to 
if(extnReason != null || extnReason != "null"){ 
    console.log("entered reason"); 
    $('#extnReason').val(extnReason); 
} 

if(extnDt != null || extnDt != "null") { 
    console.log("entered extnDt"); 
    $('#extnDt').val(extnDt); 
} 

} 

任何幫助將不勝感激!

回答

2

你需要

if(extnReason != null && extnReason != "null") 

,而不是

if(extnReason != null || extnReason != "null") 

因爲如果extnReason'null'的首要條件'null' != null將返回true,所以一個或檢查將評估爲true,因此進入塊代碼設置您的價值。

同爲其他if條件...

預加載您的領域替代辦法:你也可以只設置你的輸入標籤的value屬性,而不是使用preloadFields?即類似的:

<input type="text" id="extnDt" value="${extnDt != null ? extnDt : ''}" /> 
+0

感謝您的快速回復!出於某種原因,我認爲'||'會聲明要麼進入該操作必須是真實的,並且'&&'意味着兩者必須成爲真正的才能進入操作。我的代碼不允許使用替代方法。非常感謝您的幫助。 – Richard 2013-02-28 02:07:39

+1

沒問題,只是爲了澄清:'&&'是AND:兩個條件必須爲真,'||'爲OR,只有一個條件必須爲真。 – beny23 2013-02-28 02:09:15

+0

我明白了。爲什麼在這種情況下它既是'null'也是''null''?這讓我覺得很奇怪,因爲它表明兩者都存在爲真。我被引導認爲'null'完全沒有意義,''null''是一個'String'。 – Richard 2013-02-28 02:15:17