Jquery表单提交空白值

Jquery表单提交空白值,jquery,html,grails,gsp,Jquery,Html,Grails,Gsp,我在使用Jquery模式对话框时遇到了一些问题。我在这里试图做的是将表单提交给控制器,但是在我单击ok之后,表单将提交空白值。这是Jquery代码: <script> $(function() { var username = $( "#username" ), email = $( "#email" ), password = $( "#password" ), allFields = $( [] ).add( username ).

我在使用Jquery模式对话框时遇到了一些问题。我在这里试图做的是将表单提交给控制器,但是在我单击ok之后,表单将提交空白值。这是Jquery代码:

<script>
  $(function() {
    var username = $( "#username" ),
      email = $( "#email" ),
      password = $( "#password" ),
      allFields = $( [] ).add( username ).add( email ).add( password ),
      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;
      }
    }

    $( "#create1" ).dialog({
      autoOpen: false,
      height: 300,
      width: 350,
      modal: true,
      buttons: {
        "Create an account": function() {
          var bValid = true;
          allFields.removeClass( "ui-state-error" );

          bValid = bValid && checkLength( username, "username", 3, 16 );
          bValid = bValid && checkLength( email, "email", 6, 80 );
          bValid = bValid && checkLength( password, "password", 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." );
          // From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
          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" );
              $('#createUser').submit();

          }
        },
        Cancel: function() {
          $( this ).dialog( "close" );
        }
      },
      close: function() {
        allFields.val( "" ).removeClass( "ui-state-error" );
      }
    });

    $( "#create" ).button().click(function() {
        $( "#create1" ).dialog( "open" );
      });
  });
  </script>

$(函数(){
var username=$(“#username”),
电子邮件=$(“#电子邮件”),
密码=$(“#密码”),
allFields=$([])。添加(用户名)。添加(电子邮件)。添加(密码),
提示=$(“.validateips”);
函数更新(t){
提示
.文本(t)
.addClass(“ui状态突出显示”);
setTimeout(函数(){
removeClass(“ui状态突出显示”,1500);
}, 500 );
}
函数校验长度(o、n、最小值、最大值){
如果(o.val().length>max | | o.val().length
这是html代码:

    <div id="create1" title="Create new user">
              <form id="createUser" method="get" name="createUser">
              <p class="validateTips">All form fields are required.</p>


        <label for="name">Name</label>
        <input type="text" name="username" id="username" class="text ui-widget-content ui-corner-all" />
        <label for="email">Email</label>
        <input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" />
        <label for="password">Password</label>
        <input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />
      </form>
    </div>

所有表单字段都是必需的

名称 电子邮件 密码

那么,这里出了什么问题。我一直在寻找命名问题,但没有帮助。有人知道如何解决这个问题吗?非常感谢大家。

这可能会有所帮助,请尝试从html页面获取值,例如
$(“#用户名”)。val()

我认为这里有问题,看起来您在提交表单之前清除了所有输入值

if ( bValid ) {
  $( this ).dialog( "close" );
  $('#createUser').submit();
}


我不明白,你把它放在哪里?
var username=$(“#username”)、email=$(“#email”)、password=$(“#password”),
这是做什么的?初始化jquery模式对话框中的输入字段。我似乎不知道从何处获取此输入框的值。我删除了该行并对其进行了修复。知道remove类是关于什么的吗?是否删除$(this)。对话框(“close”);我认为在提交后重新加载页面是可以的。但如果删除所有字段.val(“”.removeClass(“ui状态错误”);当用户打算关闭并重新打开对话框时,将显示带有旧值和最后一个错误的对话框。
close: function() {
  allFields.val( "" ).removeClass( "ui-state-error" );
}