我正在使用struts2-jquery-plugin-3.2.1。我正在使用它的提交按钮,我在onclick事件上调用javascript验证,但即使验证未完成,我的表单仍在提交。这是代码:
<sj:submit
value="Add"
targets="idRightMainDiv"
button="true"
buttonIcon="ui-icon-gear"
formIds="addExerDetailsForm"
onclick="return SetsRepsFormValidate()" //calling javascript validation
tabindex="7"/>
function SetsRepsFormValidate() {
var summary = "";
summary += isSets();
summary += isReps();
if (summary != "") {
dhtmlx.alert(summary);
return false;
}
else {
return true;
}
}
答案 0 :(得分:1)
我在这个链接中得到了解决方案:
将此代码放在头部 -
<sj:head jqueryui="true"/>
<script type="text/javascript">
$.subscribe('before', function(event,data) {
var fData = event.originalEvent.formData;
alert('About to submit: \n\n' + fData[0].value + ' to target '+event.originalEvent.options.target+' with timeout '+event.originalEvent.options.timeout );
var form = event.originalEvent.form[0];
if (form.echo.value.length < 2) {
alert('Please enter a value with min 2 characters');
// Cancel Submit comes with 1.8.0
event.originalEvent.options.submit = false;
}
});
$.subscribe('complete', function(event,data) {
alert('status: ' + event.originalEvent.status + '\n\nresponseText: \n' + event.originalEvent.request.responseText +
'\n\nThe output div should have already been updated with the responseText.');
});
$.subscribe('errorState', function(event,data) {
alert('status: ' + event.originalEvent.status + '\n\nrequest status: ' +event.originalEvent.request.status);
});
</script>
写了struts 2 jquery提交按钮的事件 -
<div class="type-button">
<sj:submit targets="result"
value="AJAX Submit"
timeout="2500"
indicator="indicator"
onBeforeTopics="before"
onCompleteTopics="complete"
onErrorTopics="errorState"
effect="highlight"
effectOptions="{ color : '#222222' }"
effectDuration="3000"/>
</div>`
答案 1 :(得分:0)
您确实应该对表单的onsubmit
事件添加验证,因为也可以通过按Enter键来提交表单。
您当前的验证只会在用户点击按钮时触发。