带有两个提交按钮的表单无法正常运行

时间:2016-02-18 05:38:13

标签: javascript html jsp

我正在开发一个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"> 
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:2)

您在第一个提交按钮中输入了拼写错误,但您在style属性的末尾错过了引号。因此,formaction被视为style的一部分,而不是单独的属性。

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

答案 1 :(得分:0)

这里有很多不妥之处。我不是说让你感觉不好,只是为了帮助。

  • 在HTML中,属性不应该在等号周围有空格。不确定某些浏览器是否原谅你并让它工作,但它不是标准的,谁知道,可能是一些问题的原因
    • 永远不要使用<font>元素,因为它已过时TSQL: Prevent trigger suppressing error but rolling back transaction努力使用CSS
    • 你几乎不需要&nbsp;实体,努力使用CSS
    • 在您获得更多经验之前,请尽量不要使用内联样式(这是HTML中的样式属性)。努力使用外部CSS
    • 您应该使用元素而不是<input type="submit">,因为它更具语义性

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

答案 2 :(得分:0)

以下代码对我来说很好

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

答案 3 :(得分:-1)

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