0

我有一个带有验证及其工作的创建 jquery 模态框。但是对于我的更新 jquery 模态框,它不起作用。bvalid 没有运行验证,因为我试图在取消按钮中提交表单并且它可以工作。让我给你们看一下代码。

$(function() {
      var username = $( "#username" ),
      email = $( "#email" ),
      password = $( "#password" ),
      accountexpired = $( "#account_expired" ),

      allFields = $( [] ).add( username ).add( email ).add( password ).add( accountexpired ),
      tips = $( ".validateTips" );

    function updateTips( t ) {
      tips
        .text( t )
        .addClass( "ui-state-highlight" );
      setTimeout(function() {
        tips.removeClass( "ui-state-highlight", 1500 );
      }, 500 );
    }

    function checkLength( o, n, min, max ) {
      if ( o.val().length > max || o.val().length < min ) {
        o.addClass( "ui-state-error" );
        updateTips( "Length of " + n + " must be between " +
          min + " and " + max + "." );
        return false;
      } else {
        return true;
      }
    }

    function checkRegexp( o, regexp, n ) {
      if ( !( regexp.test( o.val() ) ) ) {
        o.addClass( "ui-state-error" );
        updateTips( n );
        return false;
      } else {
        return true;
      }
    }

    $( "#edit1" ).dialog({
      autoOpen: false,
      height: 700,
      width: 500,
      modal: true,
      buttons: {
        "Update": function() {
             var bValid = true;
             allFields.removeClass( "ui-state-error" );

              bValid = bValid && checkLength( username, "Length of username must be between 3 and 16. Username may consist of a-z, 0-9, underscores, begin with a letter. ", 3, 16 );
              bValid = bValid && checkLength( email, "Length of email must be between 6 and 80. eg. ui@mail.com", 6, 80 );
              bValid = bValid && checkLength( password, "Length of password must be between 5 and 16. Password field only allow : a-z 0-9", 5, 16 );

              bValid = bValid && checkRegexp( username, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
              bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" );
              bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );


          if ( bvalid ) {
              $( this ).dialog( "close" );
              $('#editUser').submit();

          }
        },
        Cancel: function() {

          $( this ).dialog( "close" );
        }

      }

    });

至于html:

<%--Update user box --%>
            <div id="edit1" title="Update user">
                  <form id="editUser" method="post" name="editUser">
                      <p class="validateTips"><g:message code="default.error.requiredforms.message" />.</p>

                        <g:each in="${Query1}">
                <table id="editpage" style="border-collapse: collapse;">

                    <tr>
                        <td><label for="username"><g:message code="default.searchcustomer.username.message" />:</label></td>
                        <td><input type="hidden" name="username" id="username"
                            style="border: 1px solid black" value="${it.username}" /><label>${it.username}</label></td>
                    </tr>
                    <tr>
                        <td style="white-space: nowrap"><label for="account_expired"><g:message code="default.searchcustomer.accexpired.message" />:</label></td>
                        <td style="text-align: left"><input type="radio"
                            name="account_expired" id="account_expired" value="${true}"
                            ${it.account_expired == true ? 'checked="checked"' : ''}>
                            True <input type="radio" name="account_expired" id="account_expired" value="${false}"
                            ${it.account_expired == false ? 'checked="checked"' : ''}>
                            False</td>
                    </tr>

                    <tr>
                        <td><label for="password"><g:message code="default.searchcustomer.password.message" />:</label></td>
                        <td><input type="password" name="password" id="password"
                            style="border: 1px solid black" value="${it.password}" /></td>
                    </tr>

                    <tr>
                        <td><label for="email"><g:message code="default.searchcustomer.email.message" />:</label></td>
                        <td><input type="text" name="email" id="email"
                            style="border: 1px solid black" value="${it.email}" /></td>
                    </tr>

                    </table>
                    </g:each> 
                            </form>
                  </div>

我确实在其中实现了一些 ajax,但我不知道为什么验证不起作用并阻止我提交表单。有大佬能帮我解决这个吗?非常感谢你们。

4

2 回答 2

0

看来您正在循环内输出“editpage”表。如果输出多个,则最终会得到许多具有相同名称和 id 的元素,这是无效的。单击“更新”时,验证可能会查看涉及 id 的第一个字段,而不是您尝试验证的字段。

当然,所有这些都${Query1}返回不止一行。

如果对话框被删除但单击更新时表单未提交,我想在关闭对话框之前尝试提交表单也是值得的,尽管我怀疑这会有所不同,因为关闭对话框只是将其隐藏起来。

于 2013-02-07T08:50:52.050 回答
0

定义动作form

<form id="editUser" method="post" name="editUser" action="">
于 2013-02-07T08:40:07.280 回答