jquery - 将使用 jquery 选中的复选框仅设置为子元素
问题描述
如何仅将复选框设置为使用 jquery 选中的子元素?我有以下html代码
<div id="mon">
<span>Tue</span>
<input type="text" class="start" name="mon_start" placeholder="Start" value="">
<input type="text" class="end" name="mon_end" placeholder="End" value="">
<label><input type="checkbox" class="h24" value="1">24hrs</label>
</div>
<div id="tue">
<span>Tue</span>
<input type="text" class="start" name="tue_start" placeholder="Start" value="">
<input type="text" class="end" name="tue_end" placeholder="End" value="">
<label><input type="checkbox" class="h24" value="1">24hrs</label>
</div>
我尝试了以下代码,但它总是更改 div #mon 的复选框:
$('#mon, #tue input').on('change', function(){
if ($(this).find('.start').val() == '00:00' && $(this).find('.end').val() == '24:00'){
$(this).find('.h24').prop('checked', true);
} else {
$(this).find('.h24').prop('checked', false);
}
});
解决方案
选择器#mon, #tue input
选择#mon
DIV 和里面的所有输入#tue
。但是$(this).find()
当是输入时将不起作用this
,因为它们不是容器。
您应该只使用$("#mon, #tue"). The
更改event will bubble up from all the inputs in the DIV, and then you can select other elements within the DIV using
$(this)`
推荐阅读
- iphone - 是否有任何应用程序可以将 Mac 桌面作为 VR 流式传输到 iPhone
- typescript - 将函数 reduce 应用于空数组时的问题
- sql - 为什么左外连接中需要“ON”子句
- angular - 使用 ng new 命令创建项目时项目名称无效
- javascript - php cors - 无法传递凭据
- android - Mac 中的 Android Studio Emulator 未连接到互联网
- javascript - nodejs循环模块app结构
- docker - Openshift & docker - 我可以为 Minishift 使用哪个注册表?
- node.js - 使用nodejs,cheerio刮分页
- scala - 如何从 Spark Executor 获取或创建 Hadoop 客户端