<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery UI Dialog - Modal form</title>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<style>
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;
}
</style>
</head>
<body>
<div id="dialog-form" title="添加新用户">
<p class="validateTips">All form fields are required.</p>
<form>
<fieldset>
<label for="name">姓名</label>
<input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all">
<label for="email">邮箱</label>
<input type="text" name="email" id="email" class="text ui-widget-content ui-corner-all">
<label for="password">密码</label>
<input type="password" name="password" id="password" class="text ui-widget-content ui-corner-all">
</fieldset>
</form>
</div>
<div id="dialog-form1" title="修改用户"></div>
<div id="dialog-form2" title="确定删除吗?"></div>
<div id="users-contain" class="ui-widget">
<h1>Existing Users:</h1>
<table id="users" class="ui-widget ui-widget-content">
<thead>
<tr class="ui-widget-header">
<th>姓名</th>
<th>邮箱</th>
<th>密码</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td>John Doe</td>
<td>john.doe@example.com</td>
<td>johndoe1</td>
<td>
<button class="edit">修改</button>
<button class="del">删除</button>
</td>
</tr>
</tbody>
</table>
</div>
<button id="create-user">添加新用户</button>
<button id="bt">Load Data</button>
<script>
$(function() {
var dialog, form, name, email, password, dialog1, form1, dialog2, form2, tips = $(".validateTips");
function updateTips(t) {
tips.text(t).addClass("ui-state-highlight");
setTimeout(function() {
tips.removeClass("ui-state-highlight", 1500);
}, 500);
}
function addUser() {
var valid = true;
if (valid) {
var userData = {
name: $("#name").val(),
email: $("#email").val(),
password: $("#password").val()
};
// 使用 Ajax 发送用户数据
$.ajax({
type: 'POST',
url: 'add_user.php', // 替换为您的后端处理URL
data: userData,
success: function(response) {
$('#users tbody').append("<tr>" +
"<td>" + userData.name + "</td>" +
"<td>" + userData.email + "</td>" +
"<td>" + userData.password + "</td>" +
"<td><button class='edit'>修改</button> <button class='del'>删除</button></td>" +
"</tr>");
dialog.dialog("close");
}
});
}
return valid;
}
dialog = $("#dialog-form").dialog({
autoOpen: false,
height: 400,
width: 350,
modal: true,
buttons: {
"添加": addUser,
"取消": function() {
dialog.dialog("close");
}
},
close: function() {
form[0].reset();
}
});
form = dialog.find("form").on("submit", function(event) {
event.preventDefault();
addUser();
});
$("#create-user").button().on("click", function() {
dialog.dialog("open");
});
$('body').on("click", '.edit', function() {
dialog1.dialog("open");
});
$('body').on("click", '.del', function() {
$(this).closest('tr').remove();
});
$('#bt').click(function() {
$.ajax({
type: 'GET',
url: 'data.json',
data: { 'username': 'lisi' },
success: function(data) {
console.log(data);
},
error: function() {
console.error('错误');
}
});
});
});
</script>
</body>
</html>
这段代码演示了如何将表单数据通过Ajax方法传递给服务器,并在表格中动态更新用户信息。在`addUser`函数中,使用`$.ajax`发送POST请求,将用户信息(姓名、邮箱、密码)发送到服务器。数据提交后,成功回调内将新用户添加到HTML表格中。请确保后端正确处理该请求(在示例中为`add_user.php`)。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |