[英]How to get the id of a checkbox in Jquery
I need to get the id from each checked checkbox. 我需要从每个选中的复选框中获取ID。 Right now I am able to get the sum which is great but I cant seem to get each id.
现在,我可以得到很大的总和,但是我似乎无法获得每个ID。
<input name="pay" class="pay_me" id="4" type="checkbox" value="6.00" />
<input name="pay" class="pay_me" id="5" type="checkbox" value="9.00" />
<input name="pay" class="pay_me" id="6" type="checkbox" value="3.00" />
<input name="pay" class="pay_me" id="9" type="checkbox" value="23.00" />
Jquery jQuery的
$(document).ready(function() {
$('.pay_me').change(
function() {
var t_payme = 0;
$('.pay_me:checked').each(function() {
t_payme += parseFloat($(this).val());
});
t_payme = parseFloat(t_payme).toFixed(2);
$('#to_payme').val(t_payme);
});
});
With this I can the sum to my textbox 这样我就可以将总和添加到我的文本框中
<input name="to_payme" id="to_payme" type="text" value="0.00" />
I also need the checked ID numbers like this 4|6|9 in an additional textbox 我还需要在其他文本框中输入经过检查的ID编号,例如4 | 6 | 9
<input name="ids_to_update" id="ids_to_update" type="text" value="" />
You can get element ID with this.id
(or $(this).prop("id")
). 您可以使用
this.id
(或$(this).prop("id")
)获取元素ID。 Use array to store IDs like here: 使用数组来存储ID,如下所示:
$(document).ready(function() {
$('.pay_me').change(function() {
var t_payme = 0, checked = [];
$('.pay_me:checked').each(function() {
t_payme += parseFloat($(this).val());
checked.push(this.id);
});
t_payme = t_payme.toFixed(2);
$('#to_payme').val(t_payme);
$('#ids_to_update').val(checked.join("|"));
});
});
DEMO: http://jsfiddle.net/rA5cv/ 演示: http : //jsfiddle.net/rA5cv/
Here you go: 干得好:
$(document).ready(function() {
$('.pay_me').change(function() {
var t_payme = 0, ids = [];
$('.pay_me:checked').each(function() {
ids.push(this.id);
t_payme += parseFloat($(this).val());
});
t_payme = parseFloat(t_payme).toFixed(2);
$('#to_payme').val(t_payme);
$('#ids_to_update').val(ids.join('|'));
});
});
Keep track of the IDs in an array: 跟踪数组中的ID:
var ids = [];
$('.pay_me:checked').each(function () {
t_payme += parseFloat($(this).val());
ids.push($(this).attr("id"));
});
//now do something with the array of IDs...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.