Jquery提交表单并刷新页面

时间:2013-01-17 06:09:09

标签: jquery forms jquery-ui grails jquery-ui-dialog

我遇到了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;
      }
    }

    $("#success").dialog({
          autoOpen: false,
          height: 700,
          width: 500,
         modal: true,
         buttons: {
             "Okay": function() {
                 $( this ).dialog( "close" );
                location.reload();   
             }


         }
        });
    $( "#create1" ).dialog({
      autoOpen: false,
      height: 700,
      width: 500,
      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" );
              $( "#success" ).dialog( "open" );
              $('#createUser').submit();

          }
        },
        Cancel: function() {
          $( this ).dialog( "close" );
        }

      }

    });

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

所以这里的问题是,我想在表单提交后提交表单提交PLUS刷新。但是,表单在提交之前甚至可以提交。我尝试过做AJAX,但是没有用。之前为什么我必须在提交表单后刷新是因为我有一个表中添加的东西列表,我必须刷新并向用户显示更新的列表。你们有什么想法我怎么做?非常感谢你。

1 个答案:

答案 0 :(得分:1)

问题有点令人困惑,因为如果你使用ajax就不需要刷新页面......

anywasy为什么你不提交你的表格成功dailog OK按钮被按下,因为即使create1对话框正在调用成功dailog ...这样你将获得成功对话框...表格将在成功后提交并且页面将刷新..

$("#success").dialog({
      autoOpen: false,
      height: 700,
      width: 500,
     modal: true,
     buttons: {
         "Okay": function() {
             $( this ).dialog( "close" );
            //location.reload();   
            $('#createUser').submit(); //here
         }


     }
    });

注意:在创建对话框中删除表单提交。