Home > Web Front-end > JS Tutorial > body text

jQuery's method of concatenating an attribute value of all selected checkboxes into a string_jquery

WBOY
Release: 2016-05-16 16:18:16
Original
1160 people have browsed it

The example in this article describes how jQuery concatenates an attribute value of all selected checkboxes into a string. Share it with everyone for your reference. The details are as follows:

Requirements:

For a group of checkboxes, when each checkbox is clicked, a certain attribute value of the currently selected checkbox is taken out and concatenated into a string, separated by commas.

Implementation method:

html part:

Copy code The code is as follows:












In the jQuery part, define a click event for each checkbox and traverse all selected checkboxes.

Copy code The code is as follows:
$(function() {
$("input[type=checkbox]").on("click", function () {
var vIds = "";
$("input:checked").each(function() {
vIds = $(this).attr('id') ",";
});
if (vIds.length > 0) {
vIds = vIds.substring(0, vIds.length - 1);
}
$('#result').text(vIds);
});
});

I hope this article will be helpful to everyone’s jQuery programming.

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template