jQuery UI实现模态表单Dialog(对话框),并通过表单的提交异步更新(jQuery ajax)数据

1.html文件对话框的实现

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>个人信息</title>
    <link rel="stylesheet" th:href="@{/css/jquery-ui.min.css}">
    <link rel="stylesheet" th:href="@{/css/jquery-ui.structure.min.css}">
    <link rel="stylesheet" th:href="@{/css/jquery-ui.theme.min.css}">
    <link rel="stylesheet" th:href="@{/css/user/updateUser.css}">
    <script th:src="@{/scripts/jquery-3.3.1.min.js}"></script>
    <script th:src="@{/scripts/jquery.validate.min.js}"></script>
    <script th:src="@{/scripts/messages_zh.min.js}"></script>
    <script th:src="@{/scripts/jquery-ui.min.js}"></script>
    <script th:src="@{/scripts/user/updateUser.js}"></script>
</head>
<body>
<b>个人信息页面</b><br>
<label>id </label><b th:text="${session.user.getUserId()}"></b><br>
<label>姓名 </label><b th:text="${session.user.getUserName()}"></b><br>
<label>Email </label><b th:text="${session.user.getUserEmail()}"></b><br>
<label>密码 </label><b th:text="${session.user.getUserPwd()}"></b><br>
<button id="create-user">修改信息</button>
<div id="dialog-form" title="修改信息">
    <p class="validateTips">所有的表单字段都是必填的。</p>

    <form id="updateUserform">
        <fieldset>
            <label for="name">名字</label>
            <input type="text" name="userName" id="name" class="text ui-widget-content ui-corner-all">
            <label for="email">邮箱</label>
            <input type="text" name="UserEmail" id="email" value="" class="text ui-widget-content ui-corner-all">
            <label for="password">密码</label>
            <input type="password" name="userPwd" id="password" value="" class="text ui-widget-content ui-corner-all">
        </fieldset>
    </form>
</div>
</body>
</html>

2.html文件引用的updateUser.js文件

$(function() {
    var name = $( "#name" ),
        email = $( "#email" ),
        password = $( "#password" ),
        allFields = $( [] ).add( name ).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( "" + n + " 的长度必须在 " +
                min + " 和 " + 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;
        }
    }

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

                bValid = bValid && checkLength( name, "username", 3, 16 );
                bValid = bValid && checkLength( email, "email", 6, 80 );
                bValid = bValid && checkLength( password, "password", 5, 16 );

                bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "用户名必须由 a-z、0-9、下划线组成,且必须以字母开头。" );
                // 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. [email protected]" );
                bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "密码字段只允许: a-z 0-9" );

                if ( bValid ) {
                   updateUser();//ajax更新用户信息
                    $( this ).dialog( "close" );
                }
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        },
        close: function() {
            allFields.val( "" ).removeClass( "ui-state-error" );
        }
    });

    $( "#create-user" )
        .button()
        .click(function() {
            $( "#dialog-form" ).dialog( "open" );
        });
});

function updateUser() {
  $.ajax({

      async : false,
      type : 'post',
      url : '/updateUser',
      data : $('#updateUserform').serialize(),
      success : function(data) {
          alert("修改成功");

      },
      error : function(data) {
          alert("修改失败");
      }
  })  ;
};

3.html文件引用的updateUser.css文件

body { font-size: 62.5%; }
label, input { display:block; }
input.text { margin-bottom:12px; width:95%; padding: .4em; }
fieldset { padding:0; border:0; margin-top:25px; }
h1 { font-size: 1.2em; margin: .6em 0; }
div#users-contain { width: 350px; margin: 20px 0; }
div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
.ui-dialog .ui-state-error { padding: .3em; }
.validateTips { border: 1px solid transparent; padding: 0.3em; }

4.后端controller层对应的ajax请求方法

@Controller
public class UserController {

    @Resource
    private IUserService userService;

  
    @RequestMapping("/updateUser")
    @ResponseBody
    public boolean updateUser(User user,HttpServletRequest request){
        return userService.updateUser(user,request);
    }
}

5.实现的效果图

jQuery UI实现模态表单Dialog(对话框),并通过表单的提交异步更新(jQuery ajax)数据

 

 

6.点击修改按钮,出现对话框

jQuery UI实现模态表单Dialog(对话框),并通过表单的提交异步更新(jQuery ajax)数据