gpt4 book ai didi

javascript - 运行一些代码的 jQuery 函数

转载 作者:行者123 更新时间:2023-12-02 16:39:11 25 4
gpt4 key购买 nike

我的页面中有 10 个复选框,我希望在选中任何复选框后运行一些代码。请看我的代码:

var width = 0;

$('#checkbox1').change(function() {
if($(this).is(":checked")) {
width+=5;
}
else{
width-=5;
}
//this is the code that i want it run after any of checkboxes clicked
if (width <= 25){do something}
if ( width > 25 && width <= 50){do something}
if ( width > 50 && width <= 75){do something}
if ( width > 75 && width <= 100){do something}
});

有没有办法创建一个函数或其他东西来在每个复选框内运行这些代码并防止重复这些代码?

感谢您的回答。但我只想在页面底部重复四个“if”,并且“width+=5”或“width-=5”不重复。

最佳答案

您可以使用复选框的通用选择器 :checkbox refrence

$('input:checkbox').change(function() {
if($(this).is(":checked")) {
width+=5;
}
else{
width-=5;
}
//this is the code that i want it run after any of checkboxes clicked
if (width <= 25){do something}
if ( width > 25 && width <= 50){do something}
if ( width > 50 && width <= 75){do something}
if ( width > 75 && width <= 100){do something}
});

关于javascript - 运行一些代码的 jQuery 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27649000/

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