首页 > 解决方案 > 如果单击复选框或未使用 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");

            }

标签: javascriptjquery

解决方案


如果输入 elm,则不检查 prop,而是检查 span 的 prop 使用此代码

//gets the input
const elm = $("span:contains('OTHERS')").siblings()[0];
if($(elm).prop('checked')) alert("here");

推荐阅读