Java 如何在不提交表单的情况下使用struts2提交标签作为按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20209645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:19:44  来源:igfitidea点击:

How to use struts2 submit tag as button without submitting the form?

javajavascriptajaxjspstruts2

提问by Siddharth Trikha

I am using Struts2 framework in my application, I have one button on my JSP page. That is

我在我的应用程序中使用 Struts2 框架,我的 JSP 页面上有一个按钮。那是

<s:submit type="button" name="btnSave" />

Now, I want this button to behave as normal HTML button type should not submit the form and execute the Scripting function on onclickevent. That function submit the form using Ajax.

现在,我希望这个按钮表现得像普通 HTML 按钮类型不应该提交表单并在onclick事件上执行脚本功能。该函数使用 Ajax 提交表单。

But what happens is Struts2 convert it to

但是发生的事情是 Struts2 将其转换为

<input type="submit" id="add_btnSave" name="btnSave" value="Save"/>

And my form is submitted.

我的表格已提交。

1) If I use the HTML button tag it will mess the GUI. As theme of my form is Ajax.

1) 如果我使用 HTML 按钮标签,它会弄乱 GUI。我的表单的主题是 Ajax。

This is a headtag with script

这是一个head带有脚本的标签

<head>
<s:head theme="ajax"/>
<script type="text/javascript">

$("btnSave").click(function(){
alert("aaa");
$.ajax({
url:
type:"POST",
dataType: "json",
error: function(XMLHttpRequest, textStatus, errorThrown){
alert('Error ' + textStatus);
alert(errorThrown);
alert(XMLHttpRequest.responseText);
},
success: function(){
alert('SUCCESS');
}
});
});
</script>
</head>

My bodytag is as followers :

我的body标签是关注者:

<body>
<table border="1" width="80%" align="center">
<tr>
<td width="100%">
<s:tabbedPanel id="EmpDetail" useSelectedTabCookie="true">
<s:div id="one" label="Emp Reg." theme="ajax" tabindex="0" labelposition="top">
<center>
<s:form name="frmEmpReg" namespace="/" method="post">
EMPLOYEE REGISTRATIOM TAB<br>
<s:actionmessage />
<input type="hidden" name="empbean.id" value="<s:property value="empbean.id"/>"/>
<s:textfield label="Employee First Name" name="empbean.firstName"></s:textfield>
<s:textfield label="Employee Middle Name" name="empbean.middleName"></s:textfield>
<s:textfield label="Employee Last Name" name="empbean.lastName"></s:textfield>
<s:textfield label="Address" name="empbean.address"></s:textfield>
<s:textfield label="State" name="empbean.state"></s:textfield>
<s:textfield label="Employee Designation" name="empbean.designation"></s:textfield>
<s:submit name="btnSave" type="submit" value="Save" align="center"/>
</s:form>
</center>
</s:div>
<s:div>
..
..
Other Tabs
</s:div>
</s:tabbedPanel>
</td>
</tr>
</table>
</body>

any one have any idea of handling it with Struts2 then please help.

任何人有任何想法用 Struts2 处理它然后请帮助。

Your help will be really appreciated.

您的帮助将不胜感激。

采纳答案by Roman C

If you are using s:submittag the rendered HTMLoutput is either inputor buttontag but the type is submitexcept for the imagetype. You can use the code to execute javascript onclick event that doesn't submit the form.

如果您使用s:submit标签,则呈现的HTML输出是inputbutton标签,但类型submit除外image。您可以使用代码执行不提交表单的 javascript onclick 事件。

<s:submit type="button" onclick="return false;"/>

回答by anupam

Have you tried preventDefault()?

你试过preventDefault()吗?

$("btnSave").click(function(e){
    e.preventDefault();   
    //..rest of the code
});