Javascript 未选中jQuery时,复选框仍显示true
好的,所以我目前对$.prop('checked')功能有一个问题。当取消选中某些“我的”复选框并使用此函数读取复选框时,所有复选框仍显示为true,而其中一些复选框应显示为未选中。下面是检查这一点的函数部分,但有一些背景:我使用一个表,每个td元素中都有输入值,由于它的编写方式,我必须使用td.each()函数收集所有信息/validate/和检查Javascript 未选中jQuery时,复选框仍显示true,javascript,jquery,checkbox,Javascript,Jquery,Checkbox,好的,所以我目前对$.prop('checked')功能有一个问题。当取消选中某些“我的”复选框并使用此函数读取复选框时,所有复选框仍显示为true,而其中一些复选框应显示为未选中。下面是检查这一点的函数部分,但有一些背景:我使用一个表,每个td元素中都有输入值,由于它的编写方式,我必须使用td.each()函数收集所有信息/validate/和检查 $("td", ele).each(function(idx){ var before = $('.e_cont
$("td", ele).each(function(idx){
var before = $('.e_content', this),
b_name = $('input:last[type!="hidden"], textarea:last, checkbox:last, select:last', this).attr('name'),
b_val = $('input[name="'+b_name+'"], select:last, textarea[name="'+b_name+'"]', this).val(),
b_chx = $('input:checkbox[name="'+b_name+'"]', this).prop('checked'),
after = function(){
before.hide();
$(ele).css("background", color);
$('td.edit', ele).show();
$('td.save', ele).hide();
$('span', this)
// WORKING ON TAKING THE VALUE OF THE .e_content FORM AND REPLACING THE SPAN WITH IT
.html(function(){
console.log(b_name+' : '+b_chx);
if(b_val != undefined && b_val != ''){
if(b_name == 'StageType'){
if(b_val == 1){ return 'Voice'; }
if(b_val == 2){ return 'Text'; }
if(b_val == 3){ return 'Email'; }
}
else if(b_name == 'qtrhour') {
return $('select', before).find(':selected').text();
}
else if(b_chx == true) { return '✓'; }
else if(b_chx == false) { return '✗'; }
else {
if(before.find('input:last').prop('type') != 'checkbox')
return b_val.replace(/\n\r?/g, '<br />');
}
}
})
.show();
};
$(this).html(after);
});
即使在按下save按钮之前取消选中复选框,它也始终为true。此函数在.save click事件上激发。希望这足以确定可能出现的问题。您可以尝试以下方法
$('input:checkbox[name="'+b_name+'"]', this).is(':checked');
为了避免有关选中或取消选中复选框的问题,我通常使用jQuery.attr()
有时我也会选中或取消选中它们绑定或触发.click()函数。不幸的是,它得到的值与.prop('checked')相同。这一切都是真的,这就是我困惑的地方。
$("td", ele).each(function(idx){
var before = $('.e_content', this),
b_name = $('input:last[type!="hidden"], textarea:last, checkbox:last, select:last', this).attr('name'),
b_val = $('input[name="'+b_name+'"], select:last, textarea[name="'+b_name+'"]', this).val(),
b_chx = $('input:checkbox[name="'+b_name+'"]', this).prop('checked'),
after = function(){
before.hide();
$(ele).css("background", color);
$('td.edit', ele).show();
$('td.save', ele).hide();
$('span', this)
// WORKING ON TAKING THE VALUE OF THE .e_content FORM AND REPLACING THE SPAN WITH IT
.html(function(){
console.log(b_name+' : '+b_chx);
if(b_val != undefined && b_val != ''){
if(b_name == 'StageType'){
if(b_val == 1){ return 'Voice'; }
if(b_val == 2){ return 'Text'; }
if(b_val == 3){ return 'Email'; }
}
else if(b_name == 'qtrhour') {
return $('select', before).find(':selected').text();
}
else if(b_chx == true) { return '✓'; }
else if(b_chx == false) { return '✗'; }
else {
if(before.find('input:last').prop('type') != 'checkbox')
return b_val.replace(/\n\r?/g, '<br />');
}
}
})
.show();
};
$(this).html(after);
});
$(...).attr('checked')
$(...).attr('checked','checked')
$(...).removeAttr('checked')