Javascript 选中多个复选框时获取ID
我需要选中每个复选框的ID并保存到var中: 这是我的代码:Javascript 选中多个复选框时获取ID,javascript,jquery,Javascript,Jquery,我需要选中每个复选框的ID并保存到var中: 这是我的代码: <input type="checkbox" id="p1_perro" class="checks" name="p1[]" value="p1_perro" /> <label>Perro</label> <br /> <input type="checkbox" id="p1_gato" class="checks" name="p1[]" value="p1_gato"
<input type="checkbox" id="p1_perro" class="checks" name="p1[]" value="p1_perro" />
<label>Perro</label>
<br />
<input type="checkbox" id="p1_gato" class="checks" name="p1[]" value="p1_gato" />
<label>Gato</label>
<br />
<input type="checkbox" id="p1_pajaro" class="checks" name="p1[]" value="p1_pajaro" />
<label>Pájaro</label>
<br />
<input type="checkbox" id="p1_roedor" class="checks" name="p1[]" value="p1_roedor" />
<label>Roedor</label>
<br />
<input type="checkbox" id="p1_reptil" class="checks" name="p1[]" value="p1_reptil" />
<label>Reptil</label>
<br />
<input type="checkbox" id="p1_otros" class="checks" name="p1[]" value="p1_otros" />
<label id="q_1">Otros</label>
在本例中,当用户选择一个以上的复选框时,我只能获得一个id。这将为您提供变量
中所有选中复选框的id
var selected = new Array();
$('#checkboxes input:checked').each(function() {
selected.push($(this).attr('id'));
});
您正在使用jquery,那么为什么不使用:选中的选择器呢
尝试以下代码:
<fieldset id="cb-group">
<legend>Checkboxes group</legend>
<input type="checkbox" id="p1_perro" class="checks" name="p1[]" value="p1_perro" />
<label>Perro</label>
<br />
<input type="checkbox" id="p1_gato" class="checks" name="p1[]" value="p1_gato" />
<label>Gato</label>
<br />
<input type="checkbox" id="p1_pajaro" class="checks" name="p1[]" value="p1_pajaro" />
<label>Pájaro</label>
<br />
<input type="checkbox" id="p1_roedor" class="checks" name="p1[]" value="p1_roedor" />
<label>Roedor</label>
<br />
<input type="checkbox" id="p1_reptil" class="checks" name="p1[]" value="p1_reptil" />
<label>Reptil</label>
<br />
<input type="checkbox" id="p1_otros" class="checks" name="p1[]" value="p1_otros" />
<label id="q_1">Otros</label>
</fieldset>
<script type="text/javascript">
var cbSelector = "#cb-group input[type=\"checkbox\"]",
selectedIds = [];
$(cbSelector).change(function (args) {
var selectedIds = [];
$(cbSelector + ":checked").each(function (i, e) {
selectedIds.push($(e).val());
});
console.log("Selected ids : " + selectedIds.join(", "));
});
</script>
复选框组
佩罗
加图
帕亚罗
罗多
爬行动物
奥特罗斯
var cbSelector=“#cb组输入[类型=\”复选框\“]”,
selectedDS=[];
$(cbSelector).change(函数(args){
var selectedds=[];
$(cbSelector+“:选中”)。每个(函数(即,e){
选择edids.push($(e.val());
});
log(“所选ID:+selectedIds.join(“,”));
});
您可以跟踪每个选中/取消选中,但在任何单击事件上生成选中复选框列表更容易、更安全
$(".checks").on("click", function(){
var all=[];
$(".checks").each(function(){
if (this.checked)
all.push(this.id);
});
alert(all);
});
最简单的是
$('input[name*=p1]').on('change', function () {
console.log($('input[name*=p1]:checked').map(function () {
return $(this).attr('id')
}).get());
});
.live
在最新的jQuery版本中被删除,请使用.on()
和事件委派。嘿,你有没有收到你的ans/$(这个)。attr('name')
应该是$(这个)。attr('id')
嗨,谢谢你的帮助,例如,我如何显示选中的两个复选框的名称?再次感谢。
$('input[name*=p1]').on('change', function () {
console.log($('input[name*=p1]:checked').map(function () {
return $(this).attr('id')
}).get());
});