【发布时间】:2021-09-08 03:17:37
【问题描述】:
我有一个表单,其中包含一个复选框和一个单选按钮(禁用=true)。当复选框选中则禁用单选按钮为假,当复选框未选中则单选按钮被禁用
id="rp0" 中的数字 0 从循环或侧服务器获取
<input type="checkbox" id="rp0" value="1011000646">
<input type="radio" disabled="disabled" id="trx0" value="0">
let temp = [];
function prepAcc() {
$("#tableAcc").find("tr:gt(0)").remove();
$.ajax({
url: urlAjax + $("#companyId").val(),
async: true,
success: function (result) {
console.log("list acc result: " + result + ", type: " + (typeof result));
console.log(result);
temp.push(result); // temp obj
var s = "";
$.each(result.data, function (index, obj) {
// let check = true
// < input disabled = { check? disabled: enabled } >
$("#tableAcc").find('tbody')
.append($('<tr>')
.append($('<td>').append(obj.cif))
.append($('<td>').append(obj.accNumber))
.append($('<td>').append(obj.accName))
.append($('<td>').append(obj.accCategoryName))
.append($('<td>').append($('<input>')
.attr('type', 'checkbox')
.attr('id', 'rp' + index)
.attr('value', obj.accNumber)
))
.append($('<td>').append($('<input>')
.attr('type', 'radio')
.attr('disabled', 'disabled')
.attr('id', 'trx' + index)
.attr('value', 0)
))
);
if (index > 0)
s += "|";
s += obj.accId;
});
$("#allAccString").val(s);
},
error: function (xhr) {
alert("An error occured: " + xhr.status + " " + xhr.statusText);
}
});
ajdTrx();
}
问题是当复选框未选中时,收音机未禁用=false,并且它不会进入 for 循环或映射。我喜欢这个
red block is temp with temp obj
function ajdTrx() {
for (let index = 0; index < temp.length; index++) {
console.log('IN LOOP');
let checker = document.getElementById('rp' + index);
let sendbtn = document.getElementById('trx' + index);
checker.onchange = function () {
if (this.checked) {
sendbtn.disabled = false;
} else {
sendbtn.disabled = true;
}
}
}
// temp.map((data, index) => {
// console.log('IN MAP');
// let checker = document.getElementById('rp' + index);
// let sendbtn = document.getElementById('trx' + index);
// checker.onchange = function () {
// if (this.checked) {
// sendbtn.disabled = false;
// } else {
// sendbtn.disabled = true;
// }
// }
// });
// var gf = $("input[id='rp']:checked").val();
// console.log('TRX: ' + gf);
console.log('TRX: ');
console.log(temp);
}
【问题讨论】:
标签: javascript html jquery loops