gpt4 book ai didi

javascript-events - onsubmit 事件不适用于异步表单提交

转载 作者:行者123 更新时间:2023-12-04 04:34:44 27 4
gpt4 key购买 nike

我已经使用 struts 标签创建了一个表单。我已经使用 struts jquery 的 submit 标签异步提交了表单。现在在我提交表单之前,我想调用一个 javascript 函数 checkUser() ,检查是否存在一些客户端错误并阻止表单提交。带有javascript函数的表单如下:

<script type="text/javascript">
function loadXMLDoc() {

var xmlhttp;
var k = document.getElementById("register_userDetails_username").value;

var urls = "pass.jsp?ver=" + k;

if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
document.getElementById("err1").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET", urls, true);
xmlhttp.send();
}
</script>
<script type="text/javascript">
function checkuser() {
alert("hi");
var i = document.getElementById('err1').innerHTML.search("Unavailable");
alert(i);
var j= document.getElementById('err1').innerHTML.search("Min length:8");
alert(j);
if (i!= -1 || j!=-1) {
alert("hello");
return false;
}
alert("false");
return false;
}

</script>



<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<jsp:include page="menubar.jsp"></jsp:include>
<s:form action="register" method="post" theme="css_xhtml" cssClass="form1" style="margin:8px 211px" onsubmit="return checkuser()">
<div class="searchHeader">Sign Up</div>
<div id="passwordMatchError">
<s:property value="error"/>
</div>
<s:textfield label="Name" name="userDetails.name" maxlength="30"/>
<s:textfield label="Username" name="userDetails.username" onkeyup="loadXMLDoc()" maxlength="30">
<div id="err1"></div>
</s:textfield>
<s:password label="Password" name="userDetails.password" maxlength="45"/>
<s:hidden name="userDetails.role" value='U' />
<s:password name="confirmPassword" label="Confirm Password" maxlength="45"/>
<s:radio label="Sex" name="userDetails.sex" list="#{'M':'Male','F':'Female'}" />
<s:textfield label="Phone No" name="userDetails.phoneno" maxlength="10"/>
<s:textfield label="E-mail" name="userDetails.email" maxlength="45"/>
<div id="submitdivid">
<sj:submit value="SUBMIT" cssClass="orangebuttonsmall" targets="mainContent" id="sas" />
</div>
</s:form>

现在,当我在数据库中已经存在的用户名字段中输入一些值时,它会显示字符串 Unavailable在 div 中,id 为 err .

onsubmit表单事件,我已调用 checkuser方法。但与 sj:submit , onsubmit事件不起作用。任何人都可以帮助我如何调用 make this onsubmit工作我的目标是防止表单提交,如果存在字符串 UnavailableMin Length:8在 div err .也接受任何替代解决方案。

最佳答案

在头部:

<script type="text/javascript">
$.subscribe('beforeSubmit', function(event,data) {
event.originalEvent.options.submit = checkuser();
});
</script>

在您的标签中:
<sj:submit value = "SUBMIT" 
cssClass = "orangebuttonsmall"
targets = "mainContent"
id = "sas"
onBeforeTopics = "beforeSubmit"
/>

关于javascript-events - onsubmit 事件不适用于异步表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19946043/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com