Struts2 提交按钮中令人困惑的操作
我正在尝试更新其他开发人员的现有代码。我面临着行动混乱的问题。
现有:
<s:form name="f2" action="delFood.action">
<input type="submit" value="Delete" class="button" onClick="javascript:get_check_value()"/>
要更新的代码:
<input type="file" class="button" id="foodItemFile" name="foodItemFile" value="Browse ..."/>
<input type="submit" class="button" value="AddFood" onClick="callAddFood();"/>
我的 Javascript: 在我的脚本中,我尝试通过以下代码提交我的操作。
document.f2.action = "AddFoodAction.action";
document.f2.submit();
似乎当我单击[AddFood]按钮时,它总是调用[delFood.action]。
为了添加食物,我需要在调用 [AddFoodAction.action] 操作之前使用 javascript 检查一些内容。
由于限制,我无法更改现有代码。我只能在现有代码的基础上添加新代码。
那么,有什么方法可以从 javascript 调用 [AddFoodAction.action] 而不会与相同形式的其他操作混淆吗?
在此先谢谢您。
I am trying to update the existing code of other developer. I am facing the problem of confusing actions.
Existing :
<s:form name="f2" action="delFood.action">
<input type="submit" value="Delete" class="button" onClick="javascript:get_check_value()"/>
My Code to Update:
<input type="file" class="button" id="foodItemFile" name="foodItemFile" value="Browse ..."/>
<input type="submit" class="button" value="AddFood" onClick="callAddFood();"/>
My Javascript:
In my script, I try to submit my action by following code.
document.f2.action = "AddFoodAction.action";
document.f2.submit();
It seems like when I click [AddFood] button, it always call the [delFood.action].
For adding food, I need to check something with javascripts before calling the [AddFoodAction.action] action.
Due to limitations, I can't change the existing code. I can only add new codes to the existing one.
So, Any way to call [AddFoodAction.action] from javascripts without confusing with other actions of the same form ?
Thanks ahead.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里看起来像是一个 javascript 问题。确保代码
正在执行。也许
document.f2
未正确解析(也许不止一种形式具有此名称?)。这个 fiddle 显示它应该可以工作。它更改
上
onclick
处理程序内表单的操作。只是建议,不要执行
document.f2.submit();
。它是一个,因此它会在
onclick
结束时自动提交表单。Looks like a javascript problem here. Make sure the code
is executing. Maybe
document.f2
is not resolving correctly (maybe more than one form with this name?).This fiddle shows that it should work. It changes the action of a form inside a
onclick
handler on a<input type="submit">
.And just a reccomendation, don't do
document.f2.submit();
. It's an<input type="submit">
so it will submit the form automatically whenonclick
ends.最后我就是这样解决的。
1)form标签中不会有直接的动作。
2)对于删除部分,
从javascript调用删除操作,而不是直接从表单调用。
3)对于添加部分,如删除部分。检查 java 脚本中必要的内容并调用添加操作。效果很好。
另一个解决方案:
可能有直接从表单调用的常见操作。对于这种方法,只需命名您的按钮并给出值并从操作类映射这些值。并通过使用一个操作的这些值来区分多种方法。我在 coderanch 和 javaSample。谢谢。
Finally, I resolve it by doing like that.
1) There will no direct action in form tag.
2) for delete part,
call the delete action from the javascript, not directly from form.
3) for add part, like delete part. check necessary things in java scripts and call the add action. It works well.
Another Solution:
There maybe common action directly called from Form. For this approach, just name your button and give value and map those value from action class. And differentiate multiple methods by using those value from one action. I read this article at coderanch and javaSample. Thanks.