Home  >  Article  >  Web Front-end  >  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

WBOY
WBOYOriginal
2016-05-16 17:00:141262browse

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"))
Statement:
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
Previous article:Jquery implements custom tooltip sample code_jqueryNext article:Jquery implements custom tooltip sample code_jquery

Related articles

See more