如何将参数传递给在表单标记外部定义的 javascript 函数

How to pass parameters to javascript function that are defined outside the form tag?

本文关键字:外部 定义 函数 javascript 表单 参数传递      更新时间:2023-09-26

我正在尝试将参数传递给在标签外部定义的 JavaScript 函数。但是当我尝试在 JavaScript 函数中使用它时,它显示未定义。我正在使用 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;
  }
}

由于您传递的是值本身,因此不需要语句
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>

试试这样的事情。这将帮助你

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