Home > Web Front-end > JS Tutorial > Jquery method to traverse the checkbox to obtain the value of the selected item_jquery

Jquery method to traverse the checkbox to obtain the value of the selected item_jquery

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 17:00:14
Original
1323 people have browsed it

Source code:

Copy code The code is as follows:

jQuery(function($){
$("input[name='key']:checkbox").click(function(){
var ids = '';
var flag = 0;
$("#ids"). attr("value",ids);
$("input[name='key']:checkbox").each(function(){
if (true == $(this).attr(" checked")) {
ids = $(this).attr('value') ',';
flag = 1;
}
});
if(0 < flag) {
$("#ids").attr("value",ids);
return true;
}else {
alert('Please select at least one item!');
return false;
}
});
});

Function of this source code:

Get the complex value of name='key' The value of the check box, write the value of the selected item into the form of the hidden field.

Core statement:
Copy code The code is as follows:

$(" input[name='key']:checkbox").each(function(){
if (true == $(this).attr("checked")) {
ids = $(this). attr('value') ',';
}
});

In HTML, if a checkbox is checked, the corresponding tag is checked="checked" . But if you use jquery alert($("#id").attr("checked")), you will be prompted that it is "true" instead of "checked", so judge if("checked"==$("#id" ).attr("checked")) is wrong and should be written as above: if(true == $("#id").attr("checked"))
Related labels:
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template