gpt4 book ai didi

javascript - 撤消 AJAX 调用中复选框上的 PreventDefault

转载 作者:行者123 更新时间:2023-12-03 10:14:23 25 4
gpt4 key购买 nike

当用户尝试检查未选中的复选框时(如果已选中,我将不执行任何操作),我会阻止该复选框被选中并通过 AJAX 调用对其进行验证。但我不知道如何再次“检查”它。我尝试使用 $(this).trigger('click'); 但它不起作用。

我还尝试将 preventDefault 移动到 fail 条件。但这使得通话永远无法进行。我猜是因为 AJAX 调用改变了范围。

$(document).ready(function(){
$('input:checkbox').click(function(e) {
if( $(this).prop('checked') && $(this).val()=='on' )
{
e.preventDefault();

var name = $(this).attr('name');
var pivot_attendee_program = name.split(/\]\[|\[|\]/);
var data = {
'pivot' : pivot_attendee_program[0],
'attendee_id' : pivot_attendee_program[1],
'program_id' : pivot_attendee_program[2]
};

//console.log(data);

$.ajax({
type: "GET",
url: '{{ route('ageCheck') }}',
data: data,
success: function(result){

console.log('success');
if(result == 'pass'){
console.log('pass');
$(this).trigger('click');
}
else{ //result == 'fail'

console.log('fail');

}


},
error: function(result){
alert(JSON.stringify(result));
}

});

//validate Age
//ajax check age of attendee against program
//ajax check age of attendee against program_seg

}

});

});

最佳答案

使用 context: 选项将 this 传递给 success 函数,然后设置框的 checked 属性:

       $.ajax({
type: "GET",
url: '{{ route('ageCheck') }}',
data: data,
context: this,
success: function(result){
console.log('success');
if(result == 'pass'){
console.log('pass');
$(this).prop("checked", true);
}
else{ //result == 'fail'

console.log('fail');
}
},
error: function(result){
alert(JSON.stringify(result));
}
});

关于javascript - 撤消 AJAX 调用中复选框上的 PreventDefault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29954209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com