$(function(){ //登录 $("input[type=submit]").click(function(){ $.ajax({ url: "./managerLogin", type: "post", data: $("form").serialize(), dataType: "json", success: function( data ){ if(data.code == 0){ layer.msg("登录成功", { icon: 6, time: 1000 }, function(){ location.href = data.url; }) }else{ layer.open({ title: "登录失败", content: data.msg, icon: 5, anim: 6 }) } } }) return false; }) })
时间: 2024-02-19 22:00:22 浏览: 14
这段代码是用jQuery实现的一个登录功能,当用户点击提交按钮时,会通过Ajax发送post请求,请求地址为"./managerLogin",发送的数据是表单的序列化数据。服务器返回的数据是json格式的,如果返回数据中的code为0表示登录成功,弹出一个提示框,1表示登录失败,弹出一个带有错误信息的弹窗。如果登录成功,会在1秒后跳转到指定的url。
相关问题
帮我优化界面<!DOCTYPE html> <html> <head> <title>Login</title> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script> </head> <body> <h1>Login</h1> <form> <label for="username">Username:</label> <input type="text" id="username" name="username"><br><br> <label for="password">Password:</label> <input type="password" id="password" name="password"><br><br> <input type="button" id="submit" value="Submit"> </form> <script> $(document).ready(function() { $("#submit").click(function() { var username = $("#username").val(); var password = $("#password").val(); $.ajax({ url: "/login", type: "POST", data: { username: username, password: password }, success: function(response) { if (response == "success") { window.location.href = "/dashboard"; } else { alert("Invalid username or password."); } }, error: function() { alert("Error occurred."); } }); }); }); </script> </body> </html>
以下是一个优化后的界面代码:
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<style>
form {
display: flex;
flex-direction: column;
align-items: center;
margin-top: 20px;
}
label {
font-size: 18px;
margin-bottom: 5px;
}
input[type=text], input[type=password] {
padding: 10px;
border-radius: 5px;
border: none;
margin-bottom: 20px;
width: 300px;
}
input[type=button] {
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
padding: 10px 20px;
cursor: pointer;
font-size: 18px;
margin-bottom: 20px;
width: 300px;
}
input[type=button]:hover {
background-color: #3e8e41;
}
h1 {
text-align: center;
margin-top: 50px;
font-size: 36px;
}
</style>
</head>
<body>
<h1>Login</h1>
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
<input type="button" id="submit" value="Submit">
</form>
<script>
$(document).ready(function() {
$("#submit").click(function() {
var username = $("#username").val();
var password = $("#password").val();
$.ajax({
url: "/login",
type: "POST",
data: {
username: username,
password: password },
success: function(response) {
if (response == "success") {
window.location.href = "/dashboard";
} else {
alert("Invalid username or password.");
}
},
error: function() {
alert("Error occurred.");
}
});
});
});
</script>
</body>
</html>
这个优化后的界面代码做了以下改进:
1. 使用 CSS 来美化界面,让它更加美观、易于使用和可读性更高。
2. 将表单元素布局在一个垂直方向上,使界面更加整洁。
3. 对表单元素的大小、间距和样式进行微调,使它们更加一致和易于使用。
4. 将 JavaScript 代码移到一个单独的 <script> 标签中,使它更加易于维护和重用。
$(function () { $("#code").keyup(function () { $.ajax({ url:"checkCodeIsRight.action", data:"code=" + $("#code").val(), success:function (result) { if (result == "true"){ $("input[type = 'submit']")[0].disabled = false; }else { $("input[type = 'submit']")[0].disabled = true; } } }) }) })按钮怎么写
这是一个 jQuery 匿名函数,它在文档加载完成后执行。它监听 id 为 "code" 的输入框的键盘抬起事件(keyup)并发送 Ajax 请求到 "checkCodeIsRight.action" 接口,然后根据返回结果来启用或禁用提交按钮。如果想要将这段代码应用到一个按钮上,需要将其更改为监听按钮的点击事件(click),然后将其绑定到按钮上。代码如下:
HTML:
```
<button id="myButton">按钮</button>
<input type="text" id="code">
<input type="submit" disabled>
```
JavaScript:
```
$(function () {
$("#myButton").click(function () {
$.ajax({
url: "checkCodeIsRight.action",
data: "code=" + $("#code").val(),
success: function (result) {
if (result == "true") {
$("input[type='submit']").prop("disabled", false);
} else {
$("input[type='submit']").prop("disabled", true);
}
}
});
});
});
```
在这个例子中,我们将代码应用到了一个 id 为 "myButton" 的按钮上,并将输入框和提交按钮添加到了页面中。当按钮被点击时,代码中的匿名函数将会被执行。它会发送 Ajax 请求到 "checkCodeIsRight.action" 接口,并将输入框中的内容作为请求参数。当接口返回结果时,代码会根据结果来启用或禁用提交按钮。注意,这里使用了 prop() 方法来修改按钮的 disabled 属性,这是 jQuery 1.6+ 中推荐使用的方法。