问题描述
我正在使用 Struts2 开发应用程序.我的申请是网上购物.我的任务是更新和删除购物车项目.我有一个 JSP 页面,它有 4 个文本字段.我需要将这些文本字段的数据提交给 Modify
操作.更新和删除提交按钮在同一个页面,相同的形式.如何确定我在 JSP 表单上单击了哪个按钮?
I'm developing an application using Struts2. My application is for online shopping. My task is updating and removing the shopping cart item. I have a JSP page and it has 4 text fields. I need to submit these text field's data to the Modify
action. Updating and removing submit buttons are in same page, same form. How do I determine which is the button I've clicked on JSP form?
我的表格是:
<s:form action="ModifyCart">
<s:textfield readonly="true" name="shoppingCart.item.id" label="Item Code" value="% item.id}"/>
<s:textfield readonly="true" label="Item Name" value="%{item.itemName}"/>
<s:textfield name="shoppingCart.qty" value="%{qty}"/>
<s:textfield readonly="true" label="Available Quantity" name="shoppingCart.item.qty" value="%{item.qty}"/>
<s:submit type="button" label="Update" name="submit"/>
<s:submit type="button" label="Remove" name="clear"/></s:form>
重要:
我需要在每次提交时将所有文本字段的数据提交到 Action
类.
I need to submit all text field's data to the Action
class with every submit.
推荐答案
struts2如何在一个表单中添加2个提交按钮
How to add 2 submit buttons in one form in struts2
好像你已经完成了,type="button"
意味着一个 button type="submit"
是通过 Struts 标签生成的,但是你可以使用 type="submit
.对于 description 你可以参考文档.它们之间没有太大区别,因为它们中的任何一个都提交表单.如果你想执行不同的动作或动作类的方法,那么你可以使用额外的属性,如 action
或 submit
标签中的 method
.
Seems you have already done it, type="button"
means a button type="submit"
is generated via the Struts tag, but you could use type="submit
. For description you can refer the documentation. Not much difference between them, because any of them submit the form. If you want to execute different action or method of the action class then you could use additional attributes like action
or method
in the submit
tag.
如何判断jsp表单上点击的是哪个按钮
How do I determine which is the button I've clicked on jsp form
这取决于您想在客户端或服务器端的哪个位置执行此操作,或两者兼而有之.在客户端,您可以使用 javascript 来处理表单或按钮本身的事件.代码由不同的处理程序分隔,因此在处理程序中您确切知道触发事件的元素,或使用对象 target
属性来确定类似于 这个答案.在服务器端,您可以使用 input
或 button
标记的 value
属性,该属性由 name 生成并作为参数传递代码>.请注意,并非所有 HTML 浏览器都使用按钮
value
属性.
It depends on where do you want to do it on the client side or on the server side, or both. On the client side you can use javascript to handle events of the form or the button itself. The code is separated by the different handler, so in the handler you exactly know the element that triggered the event, or use an object target
attribute to determine that like in this answer. On the server side you can use a value
attribute of the input
or button
tag that is generated and passed as a parameter by name
. Note, not all HTML browsers work with the button value
attribute.
这篇关于如何在 Struts 2 中的一个表单上添加两个提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!