如何将参数传递给在form标签之外定义的javascript函数?

时间:2014-12-04 06:19:00

标签: javascript html jsp jsp-tags

我正在尝试将参数传递给在标记之外定义的javascript函数。但是当我尝试在javascript函数中使用它时,它显示undefined.i我使用alert在jsp页面中打印值在javascipt功能...请帮助

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

}

2 个答案:

答案 0 :(得分:1)

由于您传递了值本身,因此不需要声明
var OrigvalueDate=origValDate.value;
这是我写的一个小例子,它解释了情况

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

答案 1 :(得分: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>