如何在 servlet 中不使用提交按钮来提交表单
if(Category.equals("Seasonal Decorations")){
out.println("<form name='adddeco' id='adddeco' method='post'>");
out.println("<input type=hidden name=cater value=decos>" +
"<table border=0 colspan=5 >" +
"<tr><th>Name & Description of DECORATION </th><th><input type=text name=deco_itemname width=30></th></tr>");
out.println("<tr><th>Product ID </th><th><input type=text name=deco_PID width=30></th></tr></table>");
out.println("<tr><th>Provider </th><th><input type=text name=deco_provide width=30></th></tr>");
out.println("<tr><th>AVAILIBILTY</th><th><select name=DECO_AVAIL>"+
"<option>UNAVAILABLE</option>" +
"<option>AVAILABLE</option></select></th></tr>");
out.println("<tr><th>No.of DECORATION </th><th><input type=text name=deco_qnty width=30></th></tr>");
out.println("<tr><th>Shelf NO </th><th><input type=text name=deco_shelfno width=30></th></tr>");
out.println("<tr><th>Original Price </th><th><input type=text name=deco_orgprice width=30></th></tr>");
out.println("<tr><th>Selling Price </th><th><input type=text name=deco_sellprice width=30></th></tr>");
out.println(" <input type=button value=add onclick='$(#adddeco).attr(action,additem1);$(#adddeco).submit();'> <input type=reset value=clear>" +
"</form>");
上面是我在 Servlet 中的代码,以便向数据库输入值,但问题是我无法将此表单提交到下一页,因为您可以看到我没有使用“提交”按钮我正在使用 onclick='' 事件。如果“out.println();”在 JSP 中,上面的代码就可以工作。已删除,但正如您所看到的,我需要比较 category.equals("seasonaldecors")
并仅在需要时在特定代码上运行,因此如果有人知道如何克服这个特定问题,请帮帮我。
请记住,我无法使用提交按钮来提交,因为我使用的是条形码扫描仪。
if(Category.equals("Seasonal Decorations")){
out.println("<form name='adddeco' id='adddeco' method='post'>");
out.println("<input type=hidden name=cater value=decos>" +
"<table border=0 colspan=5 >" +
"<tr><th>Name & Description of DECORATION </th><th><input type=text name=deco_itemname width=30></th></tr>");
out.println("<tr><th>Product ID </th><th><input type=text name=deco_PID width=30></th></tr></table>");
out.println("<tr><th>Provider </th><th><input type=text name=deco_provide width=30></th></tr>");
out.println("<tr><th>AVAILIBILTY</th><th><select name=DECO_AVAIL>"+
"<option>UNAVAILABLE</option>" +
"<option>AVAILABLE</option></select></th></tr>");
out.println("<tr><th>No.of DECORATION </th><th><input type=text name=deco_qnty width=30></th></tr>");
out.println("<tr><th>Shelf NO </th><th><input type=text name=deco_shelfno width=30></th></tr>");
out.println("<tr><th>Original Price </th><th><input type=text name=deco_orgprice width=30></th></tr>");
out.println("<tr><th>Selling Price </th><th><input type=text name=deco_sellprice width=30></th></tr>");
out.println(" <input type=button value=add onclick='$(#adddeco).attr(action,additem1);$(#adddeco).submit();'> <input type=reset value=clear>" +
"</form>");
The above is my code in a Servlet in order to enter a value to a DB but the problem is that I'm not able to sumbit this form to the next page as you can see I'm not using a "submit" button instead I'm using an event onclick=''. The above code works in JSP if the "out.println();" is removed but as you can see that I need to compare category.equals("seasonal decorations")
and run on the specific code only when required so if anyone who knows about how to overcome this particular problem please help me out.
And please keep in mind that I cannot use a sumbit button to submit as I'm using a barcode scanner.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
避免从 Servlet 中写出 HTML,这是不好的做法。它很难正确,很难维护,而且可读性很差。这是最适合 JSP 的工作。
您的问题似乎是生成的 JavaScript 无效。如果您检查输出页面的源代码,您会发现您正在打印:
请注意,
$()
的参数周围没有引号。应该有。这更接近正确(我说更接近是因为您的 HTML 也可能无效,具体取决于您的
DOCTYPE
- 您没有关闭标签,并且属性周围没有引号):所有这些转义引号是您应该使用 JSP 的一个重要原因。
Avoid writing out HTML from a Servlet, it's bad practice. It's hard to get right, it's hard to maintain and the readability is abysmal. This is a job best suited for JSPs.
Your problem appears to be that the JavaScript being generated is invalid. If you check the source of the output page, you'll notice that you're printing out:
Note that there are no quotes around the argument to
$()
. There should be.This is closer to correct (I say closer because your HTML may be invalid too, depending on your
DOCTYPE
- you don't close the tags and you don't have quotes around attributes):All this escaping of quotes is a big reason why you should use JSPs instead.
您可能应该阅读并使用 JSTL 来使您的代码更具可读性。它允许您使用 if 语句和其他控制结构,而无需求助于 Java 代码。例子:
You should probably read about and use JSTL to make your code more readable. It allows you to use if statements and other control structures without resorting to Java code. Example: