jquery does addition, deletion, modification and query

WBOY
Release: 2023-05-08 20:53:07
Original
769 people have browsed it

With the popularization of Web applications, the demand for more timely response from the client is getting higher and higher. Simple page interaction can no longer meet the needs of the public. At this time, the JavaScript library jQuery came into being. In most cases, if you need to perform operations such as adding, deleting, modifying, and querying data on the page, jQuery is a very convenient choice. Next we will discuss how to use jQuery to implement the add, delete, modify and check functions.

1. Page preparation

On the HTML page, you need to prepare the elements required for addition, deletion, modification and query, as shown below:

   jQuery增删改查     

jQuery增删改查



ID 名称 年龄 操作
Copy after login

Which includes the form elements and data tables, as well as jQuery references and custom JavaScript files.

2. Add data

When using jQuery to add data, you need to define a form in the HTML page and add a submit button. And write an event listener in JavaScript (usually jQuery_method.js) to get the form's data and add it to the table. The code is as follows:

$(document).ready(function () { // 监听添加操作 $('#add_btn').on('click', function (e) { e.preventDefault(); // 阻止表单默认提交行为 const name = $('#add_name').val(); const age = $('#add_age').val(); addData(name, age); }); }); function addData(name, age) { // 构造表格行 const tr = $(''); tr.append(``) .append(`${name}`) .append(`${age}`); // 构造表格行中的操作列 const td = $(''); const btn_update = $(''); const btn_delete = $(''); btn_update.on('click', function () { updateData(name); }); btn_delete.on('click', function () { deleteData(name); }); td.append(btn_update).append(btn_delete); tr.append(td); // 添加到表格中 $('#table').append(tr); // 分配ID const id = $('#table tr').length; $(`#id_${name}`).html(id); }
Copy after login

In the above code, we use jQuery to listen to the submit button of the form, obtain the data entered in the form, and add two elements to the lasttdbutton elements, one for modifying data and one for deleting data. FunctionaddData()is used to add data to the table and assign an ID to each row of data.

3. Query data

Querying data can be achieved by directly operating table elements. Take querying a name as an example:

function queryData(name) { const tr = $(`#table tr td:nth-child(2):contains(${name})`).parent(); return tr; }
Copy after login

In the above code, we use jQuery's selector syntax to select all rows of data that contain the specified name, and pass the.parent()method Returns thetrelement it is located in.

4. Modify data

To modify data, you first need to query the data that needs to be modified, and then display it on a modal box (usually a form), waiting for the user to input the data that needs to be modified. value and submit the modified data. After adding an event listener for closing the modal, the data is updated in the table based on the value entered by the user. The code is as follows:

function updateData(name) { const tr = queryData(name); const old_name = tr.find('td').eq(1).text(); const old_age = tr.find('td').eq(2).text(); const $form_update = $(` 


`); $('#table').after($form_update); $form_update.on('submit', function (e) { e.preventDefault(); const new_name = $('#update_name').val(); const new_age = $('#update_age').val(); tr.find('td').eq(1).text(new_name); tr.find('td').eq(2).text(new_age); tr.attr('id', `id_${new_name}`); $form_update.remove(); }); $('#close_btn').on('click', function () { $form_update.remove(); }); }
Copy after login

In the above code, we use jQuery selector syntax to select the data that needs to be modified and assign it an ID. Then, the data that needs to be modified is displayed to the user by popping up a modal box, allowing the user to make modifications. After the modification is completed, use jQuery to find the row again and update the data in the table. The operation of closing the button is also achieved by closing the modal box.

5. Delete data

For deleting data, you still need to display the data that needs to be deleted to the user through a modal box, and then listen to the click event on the delete button and add it to the table Delete this line. The code is as follows:

function deleteData(name) { const tr = queryData(name); const $form_delete = $(` 
`); $('#table').after($form_delete); $form_delete.on('submit', function (e) { e.preventDefault(); tr.remove(); $form_delete.remove(); }); $('#close_btn').on('click', function () { $form_delete.remove(); }); }
Copy after login

In the above code, we use jQuery's selector syntax to select the data that needs to be deleted, and display the data that needs to be deleted to the user by popping up a modal box. After the user clicks OK, the row of data is deleted from the table. For the operation of the cancel button, the modal box is still closed.

6. Summary

Through the above code, we can find that it is very easy to use jQuery to add, delete, modify and check. After the page preparation is completed, use jQuery's selector syntax to select the DOM element that needs to be operated, and use jQuery's event listener to handle the corresponding event. At the same time, jQuery's concise code also brings great convenience. Using jQuery is a very good choice for add, delete, modify and query functions that do not require complex logic.

The above is the detailed content of jquery does addition, deletion, modification and query. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!