【问题标题】:Jquery form submit blank valuesJquery表单提交空白值
【发布时间】:2013-01-16 05:54:05
【问题描述】:

我在使用 Jquery 模态对话框时遇到了一些麻烦。我在这里要做的是将表单提交给控制器,但是在我单击确定后提交空白值。这是 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>

这是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>

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

【问题讨论】:

    标签: jquery html grails gsp


    【解决方案1】:

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

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

    close: function() {
      allFields.val( "" ).removeClass( "ui-state-error" );
    }
    

    【讨论】:

    • 噢噢噢噢。我删除了这条线并修复了它。知道什么是删除类吗??
    • 你删除 $( this ).dialog( "close" );我认为如果您的页面在提交后重新加载就可以了。但是如果你删除 allFields.val("").removeClass("ui-state-error");当用户打算关闭并重新打开对话框时,您将显示带有旧值和最后一个错误的对话框。
    【解决方案2】:

    这可能会有所帮助,尝试从 html 页面获取值,例如 $( "#username" ).val();

    【讨论】:

    • 我不明白,你把它放在哪里?
    • var username = $( "#username" ), email = $( "#email" ), password = $( "#password" ),这是在做什么?
    • 在jquery modal对话框中初始化输入字段。而且我似乎无法弄清楚从哪里获得这个输入框的值