Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jQuery中,使用e.preventDefault()不会将复选框状态显示为选中状态_Javascript_Jquery - Fatal编程技术网

Javascript 在jQuery中,使用e.preventDefault()不会将复选框状态显示为选中状态

Javascript 在jQuery中,使用e.preventDefault()不会将复选框状态显示为选中状态,javascript,jquery,Javascript,Jquery,我一直试图在事件单击复选框时停止表单提交。但无法在UI中显示选中状态。我还尝试使用本文中提到的单独事件循环 首先,复选框将由ajax加载,并作为HTML追加,因此我一直在事件中使用 discountElement.rentalCompanyWithDiscountDiv .on('click', discountElement.rentalCompanyCheckBox, this.loadCamperFromCountryAndRentalCompany); 下面是将checked属性添

我一直试图在事件单击复选框时停止表单提交。但无法在UI中显示选中状态。我还尝试使用本文中提到的单独事件循环

首先,复选框将由ajax加载,并作为HTML追加,因此我一直在事件中使用

discountElement.rentalCompanyWithDiscountDiv
  .on('click', discountElement.rentalCompanyCheckBox, this.loadCamperFromCountryAndRentalCompany);
下面是将checked属性添加到HTML并通过ajax调用另一个请求的代码

loadTakeOverAndReturnableStation: function (e) {
        if (e.target.checked) {
            e.target.setAttribute('checked', 'checked');
            discountElement.camperIds.push(e.target.value);
        } else {
            e.target.removeAttribute('checked');
            discountElement.camperIds.splice(discountElement.camperIds.indexOf(e.target.value), 1);
        }
        if (discountElement.camperIds.length>0 && discountElement.rentalCompanyIds.length>0){
            var country_id = discountElement.country.val();
            var _token = discountElement._token.val();
            $.ajax({
                url: discountElement.fetchStations.val(),
                method: "POST",
                headers: {
                    'X-CSRF-TOKEN': _token
                },
                data: {
                    country_id: country_id,
                    _method: 'POST',
                    'discount_id': discountElement.discountId.val(),
                    'rental_company_id': discountElement.rentalCompanyIds,
                    'camper_id': discountElement.camperIds
                },
                success: discountModule.fillDiv
            });
        }else{
            discountElement.takeOverStationsDiv.empty();
            discountElement.returnableStationsDiv.empty();
        }
}
这是我的初始代码状态,每次单击都会重新提交表单。当我添加
e.preventDefault()时它不会在UI中显示选中的状态,但会像选中一样工作

当我尝试在setTimeout上实现checkedstate时,正如上面的回答所建议的那样

e.preventDefault();
setTimeout(function () {
if (e.target.checked) {
    e.target.setAttribute('checked', 'checked');
    discountElement.camperIds.push(e.target.value);
} else {
    e.target.removeAttribute('checked');
    discountElement.camperIds.splice(discountElement.camperIds.indexOf(e.target.value), 1);
}
},1);

它不起作用,无论是在UI中显示选中项还是在后台显示选中项,

您能详细说明一下吗?此事件正在侦听的按钮是什么类型的?它是一个提交类型按钮还是一个普通按钮?@Ekown它不是一个按钮。这是一个复选框,点击这些事件就会触发。哦,但是为什么表单是由复选框提交的呢?感觉怪怪的,哈哈
preventDefault()
将阻止事件侦听器的任何默认行为,包括复选框的可见复选标记。你能详细说明一下吗?此事件正在侦听的按钮是什么类型的?它是一个提交类型按钮还是一个普通按钮?@Ekown它不是一个按钮。这是一个复选框,点击这些事件就会触发。哦,但是为什么表单是由复选框提交的呢?感觉怪怪的,哈哈
preventDefault()
将阻止事件侦听器的任何默认行为,包括复选框的可见复选标记。