带有两个提交按钮的表单无法正常工作

Form with two submit button doesn't function properly

本文关键字:常工作 工作 表单 按钮 两个 提交      更新时间:2023-09-26

我正在开发一个Web应用程序,并且在我的jsp页面中设计了一个表单。我有一些文本字段和两个提交按钮"发送"和"搜索"。我希望我的表单在按下这些按钮时执行两个不同的操作。但是我单击搜索按钮的操作与单击发送按钮的操作相同,请帮助我。下面是我代码的一部分。

<form name="Field_Details" action="ServletApp" method="get">
  <fieldset style="float: center; width:920px; height: 75px;background-color:ivory; border-color:black;">
    <font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MachId :</font> 
    <input type="text" name="Text2" maxlength="15" style="height:15px; width:100px; border-color:black"><font size="2"></font>
    <font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;From Date(dd/mm/yy) :</font> 
    <input type="text" name="Text3" maxlength="8" style="height:15px; width:100px; border-color:black"><font size="2"></font>
    <font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;To Date(dd/mm/yy) :</font> 
    <input type="text" name="Text4" maxlength="8" style="height:15px; width:100px; border-color:black"><font size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>
    <input type="submit" value="Search" style="height:30px; width:80px; formaction="FirstServlet"/><br><br>
    <font size = "2">Output Field :</font> <input type="text" name="Text1" maxlength="50"  style="height:15px; width:100px; border-color:black"><font size = "2"></font>
    <input type= "submit" value="Send" style="height:30px; width:80px; margin-left:15px"> 

您在第一个提交按钮中有一个拼写错误,您缺少 style 属性末尾的引号。所以formaction被视为style的一部分,而不是一个单独的属性。

<input type="submit" value="Search" style="height:30px; width:80px;" formaction="FirstServlet"/><br><br>

这里有很多错误。我不是说让你难过,只是为了帮助。

  • 在 HTML 中,属性不应在等号两边有空格。不确定某些浏览器是否原谅您并让它工作,但它不是标准的,谁知道呢,可能是某些问题的原因
    • 切勿使用 <font> 元素,因为它已经过时 https://developer.mozilla.org/en-US/docs/Web/HTML/Element/font 努力改用 CSS
    • 你几乎永远不需要&nbsp;实体,努力使用 CSS 代替
    • 在你变得更有经验之前,尽量不要使用内联样式(这是HTML中的style属性)。努力改用外部 CSS
    • 你应该使用元素而不是<input type="submit">因为它更具语义性

老实说,这些都不一定是代码不起作用的原因,但是你编写它的方式使其他人很难查看和评估正在发生的事情来帮助你。

下面的代码对我来说很好用

<form name = "Field_Details" action="ServletApp" method= "get">
    <fieldset style="float: center; width:920px; height: 75px;background-color:ivory; border-color:black;">
    <font size = "2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MachId :</font> <input type="text" name="Text2" maxlength="15"  style="height:15px; width:100px; border-color:black"><font size = "2"></font>
    <font size = "2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;From Date(dd/mm/yy) :</font> <input type="text" name="Text3" maxlength="8"  style="height:15px; width:100px; border-color:black"><font size = "2"></font>
    <font size = "2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;To Date(dd/mm/yy) :</font> <input type="text" name="Text4" maxlength="8"  style="height:15px; width:100px; border-color:black"><font size = "2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>
    <input type="submit" value="Search" style="height:30px; width:80px;" onclick='this.form.action="FirstServlet";'/><br><br>
    <font size = "2">Output Field :</font> <input type="text" name="Text1" maxlength="50"  style="height:15px; width:100px; border-color:black"><font size = "2"></font>
    <input type= "submit" value="Send"  style="height:30px; width:80px; margin-left:15px";/> 

你可以写这个提交按钮。我只对一个提交按钮使用了它,因为我希望我的第二个提交按钮操作与表单操作相同。

<input type="submit" value="Search" style="height:30px; width:80px;"   onclick='this.form.action="FirstServlet";'/><br><br>
<form name = "Field_Details" action = "ServletApp" method= "get">
<fieldset style="float: center; width:920px; height: 75px;background-color:ivory; border-color:black;">
<font size = "2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MachId :</font> <input type="text" name="Text2" maxlength="15"  style="height:15px; width:100px; border-color:black"><font size = "2"></font>
<font size = "2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;From Date(dd/mm/yy) :</font> <input type="text" name="Text3" maxlength="8"  style="height:15px; width:100px; border-color:black"><font size = "2"></font>
<font size = "2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;To Date(dd/mm/yy) :</font> <input type="text" name="Text4" maxlength="8"  style="height:15px; width:100px; border-color:black"><font size = "2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>
<input type= "submit" value="Search" onclick="submitForm('FirstServlet');" style="height:30px; width:80px; formaction="FirstServlet"/><br><br>
<font size = "2">Output Field :</font> <input type="text" name="Text1" maxlength="50"  style="height:15px; width:100px; border-color:black"><font size = "2"></font>
<input type= "submit" value="Send" onclick="submitForm('ServletApp');" style="height:30px; width:80px; margin-left:15px">