js实现模糊搜索

xiaoxiao2021-02-27  291

功能一:关键字搜索

总结:1、搜索出的结果,前台先要清空原有表格 $("#tbody").empty;

           2、后台返回的json格式字符串 js eval 专成对象var stus = eval("(" + msg + ")");,在循环进行字符串拼接到表格上  $("#tbody").html(str);

$("#btn").click(function() { var id = $.trim($("#id").val()); var classname = $.trim($("#classname").val()); var clazz = $.trim($("#class").val()); if (id == "" & classname == "" & clazz == "") { alert("请至少输入一个筛选条件"); } else { $.ajax({ type : "post", url : "SearchServlet", data : { "type" : "find", "id" : id, "classname" : classname, "class" : clazz }, success : function(msg) { if ("无结果" == msg) { alert(msg); $("#id").val(""); $("#classname").val(""); $("#class").val(""); } var stus = eval("(" + msg + ")"); $("#tbody").empty; var str = ""; for (var i = 0; i < stus.length; i++) { str = str + "<tr><td>" + stus[i].id + "</td>" str = str + "<td>" + stus[i].studentNo + "</td>" str = str + "<td>" + stus[i].studentName + "</td>" str = str + "<td>" + stus[i].studentClass + "</td>" str = str + "<td>" + "<a href='javascript:a(" + stus[i].id + ")'>" + "删除" + "</a>" + "</td></tr>" } $("#tbody").html(str); } } ); } });

功能二:input框删除关键字,动态显示所有结果

总结:1、a标签动态传参,传给后台进行删除,使用字符串拼接str = str + "<td>" + "<a href='javascript:a(" + stus[i].id + ")'>" + "删除" + "</a>" + "</td></tr>"

$(".input").on('input', function(e) { var id = $.trim($("#id").val()); var classname = $.trim($("#classname").val()); var clazz = $.trim($("#class").val()); if (id == "" && classname == "" && clazz == "") { $.ajax({ type : "post", url : "SearchServlet", data : { "type" : "all", }, success : function(msg) { var stus = eval("(" + msg + ")"); $("#tbody").empty; var str = ""; for (var i = 0; i < stus.length; i++) { str = str + "<tr><td>" + stus[i].id + "</td>" str = str + "<td>" + stus[i].studentNo + "</td>" str = str + "<td>" + stus[i].studentName + "</td>" str = str + "<td>" + stus[i].studentClass + "</td>" str = str + "<td>" + "<a href='javascript:a(" + stus[i].id + ")'>" + "删除" + "</a>" + "</td></tr>" } $("#tbody").html(str); } } ); } });

功能三:删除 table中的数据

总结:

       在a标签里面传参 js写在a标签里面的两种方式

<td><ahref="javascript:a(${stu.id})">删除</a></td>

<td><a href="javascript:void(0);" οnclick="remove(${stu.id })">删除</a></td>

function a(id) { $.ajax({ type : "get", url : "SearchServlet?type=delete&id=" + id, success : function(msg) { alert(msg); if ("删除成功" == msg) { $("tr").remove("#stu_" + id); } } }); }

转载请注明原文地址: https://www.6miu.com/read-4027.html

最新回复(0)