jq php mysql增删改查_jQuery实现增删改查

本文介绍了如何使用jQuery结合PHP和MySQL实现数据的增删改查功能。通过HTML表格展示数据,利用Bootstrap模态框进行编辑和添加操作。详细讲述了jQuery代码实现删除、修改、增加和查询的具体步骤,包括数据的读取、更新和显示。
摘要由CSDN通过智能技术生成

本文实例为大家分享了jQuery实现增删改查的具体代码,供大家参考,具体内容如下

jquery用的是1.11版本

css就用bootstrap吧

因为增和改用了模态框修改,所以还用了bootstrap.js实现模态框的弹出和关闭

做了个简单的表格来实现功能

HTML代码段

//表格

搜索

增加

编号姓名成绩操作
1张三89

修改

删除

2李四91

修改

删除

3刘一80

修改

删除

//修改的模态框

//增加的模态框

Jquery代码段

//删除的功能

$(document).on("click", ".del", function() {

$(this).parents("tr").remove()

})

//改的功能

var _this = null

$(document).on("click", ".rev", function() {

var _arr = []

_this = $(this).parents("tr")

$(this).parents("tr").find("td:not(:last)").each(function(){

_arr.push($(this).text())

})

$("#myModal").find("input").each(function(i){

$(this).val(_arr[i])

})

})

$(document).on("click",".olk", function(){

var _arr = []

$("#myModal").find("input").each(function(){

_arr.push($(this).val())

})

_this.find("td:not(:last)").each(function(i){

$(this).text(_arr[i])

})

})

//增加的功能

$(document).on("click",".aad",function(){

var _arr = []

var str = ""

$("#myModel").find("input").each(function(){

_arr.push($(this).val())

})

str = '

'+_arr[0]+''+_arr[1]+''+_arr[2]+'修改 删除'

$(".table").append(str)

})

//查的功能

$(".sreach").click(function(){

var oS = $(".swich").val()

if(oS.length==0){

alert("请输入点东西")

}else if($("table tr td:contains('"+oS+"')").length==0){

alert("找不到数据")

}else{

$(".table tr:not(:first)").hide()

$(".table tr:contains('"+oS+"')").show().find("input").prop("checked",true)

}

})

ps:新人,class的命名有点不规范...将就看着吧

解说思路

ps:要记得对象缓存 _this = $(this).null

1.实现删的功能

首先准确地找到当前按钮的父级元素tr,然后remove()掉就实现了删的功能

2.实现改的功能

这里先做了个数组来存储已有的信息, 用遍历的方法each()放进数组,数组的数据再push()进模态框的input框val()可进行显示

点击模态框的确认按钮才能实现更改,所以又要重新将已更改的input框的val()重新遍历进另外的一个数组进行存储,再push()进表格就实现更改的更改了

3.实现增的功能

增加的功能也用了模态框来采集数据,所以也用一个数组来存储数据,将已采集的input框val()遍历进数组,创建一个命名为str的dom节点,用数组下标来插入要追加的dom节点,增加的功能就实现了

4.实现查的功能

首先要获取搜索框里val(), 判断搜索框的长度是否为0,假如是0就弹出“请输入点东西”,再用contains()方法判断搜索框的内容在表格里的有没有,没有就弹出“找不到数据”,再或者搜素框的内容在表格里有就把除了第一行的数据hide(),将表格里有和val()一样的tr show()出来

整个table的增删改查的功能就实现啦。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 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、付费专栏及课程。

余额充值