繁体   English   中英

<a>在jsp中</a>使用标记提交表单<a>不起作用</a>

[英]form submission using tag <a> in jsp not working

我试图通过使用html标记将表单从一个jsp提交到另一个jsp,如下所示:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>SendIt Bank</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link href="css/bank.css" rel="stylesheet" type="text/css">
<script language="JavaScript" type="text/JavaScript">

function checkUserInListAndSubmit() { 
    alert("hello");
    document.getElementById('userLoginForm').submit();
    //document.forms["userLogin"].submit();

}

</script>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<form name="userLoginForm" id="userLoginForm" action="accountdetails.jsp" method="POST">
    <table width="202" height="154" border="0" align="center" cellpadding="5" cellspacing="0" bgcolor="#CCCCCC"> 
      <tr>
        <td colspan="2" valign="bottom" class="headermain">&nbsp;</td>
      </tr>
      <tr>
        <td colspan="2" valign="bottom" class="headermain"><div align="center" class="style4">Online
            Banking Login</div></td>
      </tr>
      <tr>
        <td class="loginbox style5">User ID:</td>
        <td class="loginbox"><input name="userID" type="text" id="userID" size="12" maxlength="20"></td>
      </tr>
      <tr>
        <td width="68" class="loginbox style5">Password: </td>
        <td class="loginbox"><input name="password" type="password" id="password" size="12" maxlength="20"></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td width="103"><div align="left"><a href="JavaScript:checkUserInListAndSubmit()"><img src="images/btn-signin.png" alt="Sign In" name="submit" width="60" height="20" border="0" align="right"></a></div></td>
      </tr>
      <tr>
        <td colspan="2"><p align="center"><span class="style2"><a href="FYP1.html">Forgot
                your Password?</a><br>
        </span> </p></td>
      </tr>
    </table>
</form>
</body>
</html>

当我单击href链接时,就我而言,我正在单击图像。 控制转到javascript函数()。 但是,该表单未提交。 当我尝试在Chrome中调试时,在控制台中看到此错误。

Uncaught TypeError: object is not a function index.jsp:12
checkUserInListAndSubmit index.jsp:12
(anonymous function)

错误出现在document.getElementById('userLoginForm')。submit();行中 请帮助我解决此问题并提交表格。

您是否不使用输入type =“ submit”的任何原因?

反正这就是你的答案

<form id="userLoginForm'" action="" method="post">
    <a href="JavaScript:checkUserInListAndSubmit()"><img src="images/btn-signin.png" alt="Sign In" name="submit" width="60" height="20" border="0" align="right" onclick="document.getElementById('userLoginForm'').submit();"></a>
</form>

onclick="parentNode.submit();

应该管用

尝试使用此锚标记,

<a href="javascript:{}" onclick="document.getElementById('userLoginForm').submit();">

或者,如果您想做更多的js函数,

    <a href="javascript:{}" onclick="checkUserInListAndSubmit();return false;">

更新的答案

  1. 用id指定您的锚,例如;

     <a href="#" id="mylink"> 
  2. 为锚标记onclick侦听器编写js函数

     document.getElementById("mylink").onclick = function() { //do other coding here document.getElementById("userLoginForm").submit(); return false; } 

在标记中,删除/重命名属性

name="submit"

那是,

  <tr>
    <td>&nbsp;</td>
    <td width="103"><div align="left"><a href="JavaScript:checkUserInListAndSubmit()"><img src="images/btn-signin.png" alt="Sign In" width="60" name="some-other-name" height="20" border="0" align="right"></a></div></td>
  </tr>

检查此答案以获取更多信息。

干杯!

暂无
暂无

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

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