简体   繁体   English

将函数从.js传递到HTML

[英]Passing functions from .js to HTML

Would something like this work? 这样的事情行吗?

.js .js文件

var error = "";

function textBox(checkForBlanks)
{
    if (checkForBlanks == "")
    {
        error(errorMessage);
    }
}

HTML HTML

<!DOCTYPE html>
<html>
<head>
   <script type="text/javascript" src="new.js"></script>
   <script>
function validateAll()
{
   var first = document.billingForm.first.value;
   var errorMessage = "";

   if (first == textBox)
   {
        errorMessage += "Enter name";
   }
   if (errorMessage != "")
   {
        alert(errorMessage); or
            alert(error);
   }
} 
 </script>
</head>
<body>
<form action="http://cswin2k5/echo/default.asp" method="post" name="billingForm">
<p>First Name*:
<input type="textbox" name="first" id="first" onBlur="this.value=trim(this.value)" onkeyup='capitalize(this)'></p>

<p><input type="button" value="Proccess Payment" onClick="return validateAll();"/></p>
   </form>
</body>
</html>

Or something like this? 或类似的东西? What this is trying to do is check if the text box is empty, that way I can use the same function for all text box. 这样做是要检查文本框是否为空,这样我就可以对所有文本框使用相同的功能。 I'm trying to pass this through to HTML for validation. 我正在尝试将其传递给HTML进行验证。

Try: 尝试:

function validateForm()
{
  var x=document.forms["billingForm"]["first"].value;
  if (x==null || x=="")
  {
    alert("Empty field");
    return false;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM