我有一个复选框列表,如下所示:
$('.check-user').click(function() {
var $this = $(this);
var user_id = $this.attr('value');
var brandContainer = $this.closest('.pop-state').siblings('.pop-brands');
brandContainer.html('<p style="margin: 10px !important;">Processing... </p>');
$.post('/user/usersBrands', { userId: user_id } , function(data) {
var brands = JSON.parse(data);
var container = $('<div class="pop-mission-co" />');
brands.forEach(function (brand, index) {
var isRegisteredToBrand = null;
if(brand.userId==null)
{
isRegisteredToBrand = false;
}
else{
isRegisteredToBrand = true;
}
container.append(buildBrand(brand.id, brand.name, isRegisteredToBrand, index));
});
function buildBrand(id, name, isActive, index) {
var isChecked = isActive ? 'checked="checked"' : ' ';
return $(
'<div class="pop-mission-to">' +
'<label>' +
'<input class="check-brand"' +
isChecked +
'type="checkbox"' +
'name=""' +
'value="' + id + '"' +
'data-id="' + index + '">'
+ name +
'</label>' +
'</div>'
);
}
brandContainer
.children('p').remove();
brandContainer
.html(container)
.find('.pop-mission-co').show();
});
brandContainer.on('change', '.check-brand', function() {
console.log($(this).attr("checked"));
});
});
复选框列表是在jQuery中的$ .post之后创建的,现在当检查这些复选框中的每个复选框时,我需要处理该事件,如下所示:
brandContainer.on('change', '.check-brand', function() {
console.clear();
var checked = "";
if($(this).attr("checked"))
{
checked = "checked";
}
else
{
checked = "unchecked";
}
console.log(checked);
console.log($(this).val());
});
如您所见,我正在使用委托事件处理程序来检查复选框是否已选中...当用户单击1复选框时,应将其放入数组中,如下所示:
Array { "checked", "1" }
当用户单击另一个复选框时,应按以下方式更新阵列:
Array { "checked", "1", "checked", "2" }
如果用户取消选中已选中的复选框,则说第二个值为2,则数组应如下所示:
数组{“ checked”,“ 1”,“ unchecked”,“ 2”}
整个过程还在继续...有人可以帮我这个忙吗?非常感谢 !:)
我认为这可能有效。
var checkbox_events = [];
brandContainer.on('change', '.check-brand', function() {
if($(this).is(':checked') {
checkbox_events[$(this).val()] = 'checked';
} else {
checkbox_events[$(this).val()] = 'unchecked';
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句