if 和 else 语句

任何

所以基本上我正在尝试为按钮创建一个 if/else 语句。当检查禁止复选框时,我想显示禁用按钮,但检查1个或更多复选框时,我想显示另一个按钮。

JS

 $("#checkAll").change(function () {
        $("input:checkbox").prop('checked', $(this).prop("checked"));
    });

html

 <input type="checkbox" id="checkAll" style="margin-left:-10px" />
 <input type="checkbox"  style="margin-left:5px" />
 <input type="checkbox"  style="margin-left:5px" />


<div class="ml-2">
               if(checked){
                <button type="button" class="btn btn-danger ">Delete</button>
               }else{
                <button type="button" class="btn btn-outline-danger" disabled>Delete</button>
                }
            </div>
自由-m

当检查禁止复选框时,我想显示禁用按钮,但检查1个或更多复选框时,我想显示另一个按钮。

您可以在 html 中包含两个按钮并隐藏一个 - 然后在您的 js 中根据需要显示/隐藏它们。

一个基本的实现将检查:

if ($("input:checkbox:checked").length == 0) {

然后.show() .hide()根据需要在每个按钮上使用。

这可以更加简化,例如使用.toggle($("input:checkbox:checked").length == 0)但这是按照标题中的要求显示明确的 if/else。

$("#checkAll").change(function() {
  $("input:checkbox").prop('checked', $(this).prop("checked"));
});

$("input:checkbox").change(function() { 
  if ($("input:checkbox:checked").length == 0) {
    $(".ml-2>.btn-danger").hide();
    $(".ml-2>.btn-outline-danger").show();
  } else {
    $(".ml-2>.btn-outline-danger").hide();
    $(".ml-2>.btn-danger").show();
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="checkAll" />
<input type="checkbox" />
<input type="checkbox" />


<div class="ml-2">
  <button type="button" class="btn btn-danger " style='display:none;' >Delete</button>
  <button type="button" class="btn btn-outline-danger" disabled>Delete (disabled)</button>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章