简体   繁体   English

如何使用 jquery 取消选中复选框

[英]how to uncheck a checkbox with jquery

I have an issue with unchecking a check box.我在取消选中复选框时遇到问题。

I have this code in my html (php)我的 html (php) 中有此代码

<div class="row">
    <div class="col-md-12 pr-1">
        <label>Modele</label>
        <div class="form-group">
            <div class="btn-group paymentBtnGroup btn-group-justified" data-toggle="buttons">
                    <?
                    if($result->num_rows > 0) {
                        $i = 1;
                        while ($row = $result->fetch_assoc()) {
                            echo "<label class='btn paymentMethod'>";
                            echo "<div class='method col-sm' style='background-image: url(" . "assets/img/produits/".$row["IMAGEPATH"].")'>";
                            echo "</div>";
                            echo "<input type='radio' aria-labelledby='".$row["NOM"]."' value='".$row["K_MODELE"]."' name='modele' id='modele$i'>";
                            echo "</label>";
                            $i++;
                        }
                    }
                    ?>
            </div>
        </div>
    </div>
</div>

and I try with a function to uncheck it once the form is added to my array.一旦表单添加到我的数组中,我尝试使用 function 取消选中它。

so in my Jquery I try to use this line所以在我的 Jquery 我尝试使用这条线

    $('.paymentBtnGroup input[type="radio"][name="modele"]').each(function(){
        $(this).prop('checked', false);
    });

but it give an undefined error for my radiobuttons and it doesn't work.但它给我的单选按钮一个未定义的错误,它不起作用。

Any suggestions?有什么建议么?

this doesn't mean what you think it means. this并不意味着你认为它意味着什么。 But you can simplify to this:但是您可以简化为:

$('.paymentBtnGroup input[type="radio"][name="modele"]').prop('checked', false);

You need to make sure that your DOM is ready before applying any jquery在应用任何 jquery 之前,您需要确保您的 DOM 已准备就绪

put script inside $(function(){});将脚本放入 $(function(){});

$(function(){
  $('.paymentBtnGroup input[type="radio"][name="modele"]').each(function(){
        $(this).prop('checked', false);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM