javascript - 如果单击复选框或未使用 javascript/jquery,如何检查条件?
问题描述
我的复选框是动态的,如果我的带有“其他”的复选框被点击,我只需要检查条件。所以我的复选框是:
<div class="form-group" id="documents">
<label> <input id="check_id1" type="checkbox" value="1" class="chk1" checked=""> <span>Invoice</span>
<br>
</label>
<div style="padding-bottom: 5px"></div>
<label> <input id="check_id2" type="checkbox" value="2" class="chk2" checked=""> <span>Packing List</span>
<br>
</label>
<div style="padding-bottom: 5px"></div>
<label> <input id="check_id3" type="checkbox" value="3" class="chk3" checked=""> <span>OTHERS</span>
<br>
</label>
<div style="padding-bottom: 5px"></div>
</div>
所以我试图检查是否选中了“其他”复选框,但没有触发警报() 。
if($("span:contains('OTHERS')").prop('checked') == true){
alert("here");
}
解决方案
如果输入 elm,则不检查 prop,而是检查 span 的 prop 使用此代码
//gets the input
const elm = $("span:contains('OTHERS')").siblings()[0];
if($(elm).prop('checked')) alert("here");
推荐阅读
- javascript - node express.static 读取所有目录
- javascript - 如何从 ajax 响应更新 ckeditor5 中的内容
- php - 检查 laravel 中的至少表单数据是否为空
- reactjs - 展开或折叠时 Material UI 自动完成旋转图标
- linux - AWK:使用字段分隔符连接匹配模式
- php - Laravel artisan 命令未正确完成
- node.js - 为事件驱动架构选择正确的协议。
- jenkins - Kubernetes master 作为 Jenkins slave
- selenium - 需要检查硒中是否存在元素
- html - 如何防止绝对定位的孙子拉伸容器scrollHeight / scrollWidth?