2

我已经使用 struts 标签创建了一个表单。我已经使用 struts jQuery 的提交标签异步提交了表单。现在,在我提交表单之前,我想调用一个 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>

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

onsubmit表单事件中,我调用了checkuser方法。但是sj:submitonsubmit事件不起作用。任何人都可以帮助我如何调用使这项onsubmit工作我的目标是防止表单提交,如果存在 StringUnavailableMin Length:8div err。也接受任何替代解决方案。

4

1 回答 1

3

在头部:

<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"
/>
于 2013-11-13T09:33:26.200 回答