如何使用复选框?为什么我的复选框没有被更改



我有一个复选框,当它改变时,我对它说为我做点什么。

当我几天前测试它工作得很好,但从昨天到现在它不工作

我没有改变我的代码,我的意思是当我没有改变我的代码过程是如何改变的?

我的jquery代码不适合我:

var tbody = '';
$.each(data, function(key, value) {
tbody +=
'<td><div class="form-group"><div class="checkbox checkbox-primary"><input class="checkbox" data-id="' +
value.id +
'" type="checkbox"><label for="checkbox1"></label></div></div></td>';
tbody += '</tr>';
});
$('tbody').append(tbody);
$(document).on('change', 'input.checkbox', function(event) {
var data = {
access_token: getCookie("access_token")
}
var checkbox = event.target;
if (checkbox.checked) {
var id = $(this).data('id');
response = ajaxRequest(
"http://localhost:8000/api/customers/mobile-numbers/is-default/" + id,
"POST", data);
if (response.status === true) {
Swal.fire({
title: 'موفقیت',
icon: 'success',
text: response.message,
confirmButtonText: 'تایید',
showCancelButton: false,
}).then((result) => {
location.reload();
});
} else if (response.status === 401) {
Swal.fire({
title: 'دسترسی غیر مجاز',
icon: 'error',
text: response.message,
showConfirmButton: false,
timer: 1500
}).then((result) => {
window.location = "http://localhost:7000/login";
});
} else {
Swal.fire({
title: 'خطا سمت سرور',
icon: 'error',
confirmButtonText: 'تایید',
showCancelButton: false,
showCloseButton: true
});
}
}
});

对于动态追加复选框,你必须使用eventlistener

下面是代码:

var checkbox = document.querySelector(".checkbox");
checkbox.addEventListener("change", (event) => {
event.preventDefault();
// Next Code
});

最新更新