gpt4 book ai didi

javascript - 功能从第一次就不起作用

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

在下面的 Jquery 代码中,checkBoxName.on('change', function() { 将在我第二次选中复选框后触发,我正在调用函数 fooonchange 中的复选框中,为什么它从我第一次选中该框时就不起作用?

function foo (arg1,arg2,arg3) {
$(document).ready(function() {
$('#checkBox').on('change', function() {

if ($('#checkBox').is(':checked')) {//this is triggered after the second time i check the box
$('#errorDiv').removeClass(errorClass);
$('#submitBtn').removeAttr("disabled");
} else {
$('#submitBtn').attr('disabled', 'disabled');
$('#errorDiv').addClass(errorClass);
}

});
});
}

最佳答案

如果您从复选框更改事件处理程序调用 foo(),则 foo() 所做的就是安装另一个更改处理程序。它不会在当前事件上触发新的处理程序。

使用 $(document).ready() 将内容包装在 foo 内部也有点奇怪。我不知道这是否会导致问题,但这不是通常的做事方式,这表明您可能正在尝试做一些您没有解释过的不寻常的事情。

可能,您只想从一开始就安装复选框处理程序:

$(document).ready(function () {

$('#checkBox').on('change', function () {

if ($('#checkBox').is(':checked')) { //this is triggered after the second time i check the box
$('#errorDiv').removeClass(errorClass);
$('#submitBtn').removeAttr("disabled");
} else {
$('#submitBtn').attr('disabled', 'disabled');
$('#errorDiv').addClass(errorClass);
}
});
});

关于javascript - 功能从第一次就不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38678396/

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