表单请求改为ajax,使用jq将form表单提交改为ajax提交

大家可能都会有这样的经历:辛辛苦苦的敲了半天的form表单,然后被通知要改成ajax表单提交,于是就开始了一行一行的获取input的值,复制、粘贴的过程,要是碰上表单行数特别多的时候更麻烦,下面介绍一种方法,用最少的代码量完成form表单提交到ajax提交的转换,下面是代码:$('#save').click(function () {

var formdata = $('form').serializeArray();

var data = {}

$.each(formdata, function() {

data[this.name] = this.value;

});

$.post('save',data,function (data) {

if (data) {

$('#tech').bootstrapTable('refresh', {});

layer.msg('添加成功');

}else{

layer.msg('添加失败');

}

});

});

是不是很简单,其中serializeArray是将form的数据提取出来,然后遍历赋值给data,然后作为post参数发送就可以了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一个不使用 jQuery 的 Ajax 增删改查实现登录注册页面的示例代码。下面是一个简单的实现: HTML: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录注册页面</title> </head> <body> <h1>登录</h1> <form id="login-form" method="post"> <label for="username">用户名:</label> <input type="text" id="username" name="username"><br> <label for="password">密码:</label> <input type="password" id="password" name="password"><br> <button type="submit">登录</button> </form> <hr> <h1>注册</h1> <form id="register-form" method="post"> <label for="new-username">用户名:</label> <input type="text" id="new-username" name="new-username"><br> <label for="new-password">密码:</label> <input type="password" id="new-password" name="new-password"><br> <label for="confirm-password">确认密码:</label> <input type="password" id="confirm-password" name="confirm-password"><br> <button type="submit">注册</button> </form> <script src="index.js"></script> </body> </html> ``` JavaScript: ```js // 获取登录表单和注册表单 const loginForm = document.getElementById('login-form'); const registerForm = document.getElementById('register-form'); // 监听登录表单提交事件 loginForm.addEventListener('submit', (event) => { event.preventDefault(); // 阻止表单默认提交行为 const username = document.getElementById('username').value.trim(); const password = document.getElementById('password').value.trim(); // 发送登录请求 const xhr = new XMLHttpRequest(); xhr.open('POST', 'http://localhost:3000/login'); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { if (xhr.status === 200) { alert('登录成功!'); } else { alert('登录失败!'); } } }; xhr.send(JSON.stringify({ username, password, })); }); // 监听注册表单提交事件 registerForm.addEventListener('submit', (event) => { event.preventDefault(); // 阻止表单默认提交行为 const newUsername = document.getElementById('new-username').value.trim(); const newPassword = document.getElementById('new-password').value.trim(); const confirmPassword = document.getElementById('confirm-password').value.trim(); // 校验密码是否一致 if (newPassword !== confirmPassword) { alert('两次输入的密码不一致!'); return; } // 发送注册请求 const xhr = new XMLHttpRequest(); xhr.open('POST', 'http://localhost:3000/register'); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { if (xhr.status === 200) { alert('注册成功!'); } else { alert('注册失败!'); } } }; xhr.send(JSON.stringify({ username: newUsername, password: newPassword, })); }); ``` 这段代码中使用了 XMLHttpRequest 对象来发送 Ajax 请求,通过设置 `open` 方法的第一个参数指定请求的方法和 URL,设置 `setRequestHeader` 方法来设置请求头,设置 `send` 方法来发送请求体。在 `onreadystatechange` 事件处理函数中,通过 `readyState` 和 `status` 两个属性来判断请求是否成功,请求成功则弹出成功提示框,请求失败则弹出失败提示框。 当然,这只是一个简单的示例,实际场景中还需要对请求参数进行校验,对响应数据进行处理等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值