不使用onsubmit以html格式调用Javascript函数

时间:2015-03-17 17:06:32

标签: javascript html jsp

我有一个非常简单的html文件,在javascript中有一个验证函数,我试图在提交表单时执行。但是,由于没有执行控制台日志语句,我无法将is_checked()函数作为证据运行。我正在使用Eclipse和Apache TomCat 7服务器,它正确执行所有jsp,servlet和html代码但没有javascript。

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Example</title>
<script type="text/javascript">
function is_checked() { 
    console.log("is_checked() was called");
    var taskChecked = document.getElementByID('task').checked;
    var projectChecked = document.getElementByID('project').checked;
    var autoChecked = document.getElementByID('auto').checked;
    var manualChecked = document.getElementByID('manual').checked;

    console.log("taskChecked " + taskChecked);
    console.log("projectChecked " + projectChecked);
    console.log("manualChecked " + manualChecked);
    console.log("autoChecked " + autoChecked);


    if((taskChecked === "true" || projectChecked === "true") 
     && (autoChecked === "true" || manualChecked === "true")) {
        return true;
    } else {
        alert('Must select task or project and either auto or manual!');
        return false;
    }
}
</script>
</head>
<body>
<form action="#" onsubmit="return is_checked()" method="post">
<table>
    <tr>
      <td>Process Level: </td>
      <td>
       <input type="radio" name="process" value="task" id="task">Task
      </td>
      <td>
         <input type="radio" name="process" value="project"
          id="project">Project
    </td>
    </tr>
    <tr>
        <td>Method Type: </td>
        <td>
         <input type="radio" name="method" value="auto" id="auto">Auto
       </td>
        <td>
         <input type="radio" name="method" value="manual" 
             id="manual">Manual
      </td>
    </tr>
    <tr>
        <td colspan="2">
            <textarea name="ids" rows="15" cols="10"></textarea>
        </td>
    </tr>
    <tr>
        <td>
         <input type="submit" value="Submit">
       </td>
    </tr>
</table>
</form> 
</body>
</html>

1 个答案:

答案 0 :(得分:0)

你有一个错字: document.getElementByID应替换为document.getElementById

顺便说一句,您正在调用该函数两次,您可以删除提交输入中的onClick