2014-12-04 65 views
1

我想將一個參數傳遞給一個在tag之外定義的javascript函數,但是當我嘗試在javascript函數中使用它時,它顯示undefined.i使用警報在jsp頁面和SCRIPT關閉......請幫助如何將參數傳遞給在窗體標籤外定義的javascript函數?

<html> 
<script type="text/javascript"> 
js_valueDate = '<%=valueDate%>'; 
alert(js_valueDate)  **//displays correct value here** 

</script> 
    <body> 
    <form> 
....some html... 
    <td width=27%><input type=text name="ValDate" 
onchange = "javascript:validateDate(document.f1.ValDate,js_valueDate);"></td> 
......some html....                        
    </form> 
    </body> 
    </html> 

,這是我的javascript函數:

function validateDate(ValDate,origValDate) { 
    var valueDate=ValDate.value; 
    var OrigvalueDate=origValDate.value; 
    confirm(valueDate); 
    confirm(OrigvalueDate); **//displays undefined here** 
    var hh=replaceAll(valueDate,'-',''); 
    confirm(hh); 

    if (replaceAll(valueDate,"-","")<=valueDate<=replaceAll(OrigvalueDate,"-","")) { 
    return true; 
    } else { 
    alertPopup("Please enter a valid value date"); 
    document.f1.ValDate.focus(); 
    return false; 
    } 

} 
+0

只是把包含表單標籤之前,在頁面頂部的JavaScript函數的腳本標籤,並檢查.. – tarzanbappa 2014-12-04 06:39:55

回答

1

由於要傳遞的價值本身就沒有必要聲明
var OrigvalueDate=origValDate.value;
這裏是一個小例如, h我已經寫了解釋情況

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
    <head> 
    <title>Check </title> 
    <script> 
    function display(v) 
    { 
    var d=v.value; 
    alert(v); 
    alert(d); 
    } 
    jval="qwerty"; 
    </script> 
    </head> 
    <body> 
    <input type="button" value="check" onclick="javascript:display(jval)"/> 
    </body> 
</html> 
+0

你是絕對正確....感謝您的答覆 – rydz 2014-12-04 06:44:50

0

嘗試類似這樣的事情。這會幫你

<html> 
    <script type="text/javascript"> 
    js_valueDate = '<%=valueDate%>'; 
    alert(js_valueDate)  **//displays correct value here** 

var ValidationHandler = { 

validateDate:function(ValDate,origValDate){ 
    var valueDate=ValDate.value; 
    var OrigvalueDate=origValDate.value; 
    confirm(valueDate); 
    confirm(OrigvalueDate); **//displays undefined here** 
    var hh=replaceAll(valueDate,'-',''); 
    confirm(hh); 



    if (replaceAll(valueDate,"-","")<=valueDate<=replaceAll(OrigvalueDate,"-","")) 
    { 
    return true; 
    } 
    else 
    { 
    alertPopup("Please enter a valid value date"); 
    document.f1.ValDate.focus(); 
    return false; 
    } 

} 
}; 

</script> 
    <body> 
    <form> 
....some html... 
    <td width=27%><input type=text name="ValDate" 
onchange = "javascript:ValidationHandler.validateDate(document.f1.ValDate,js_valueDate);"></td> 
......some html....                        
    </form> 
    </body> 
    </html> 
相關問題