检查是否在引导程序中选中了复选框

Check whether checkbox is selected or not in bootstrap

本文关键字:复选框 引导程序 是否 检查      更新时间:2023-09-26

我从这里使用检查列表组 - http://bootsnipp.com/snippets/featured/checked-list-group

我想选中哪个复选框,并且我想使用按钮单击重置所有选中的项目。我想在Javascript中做到这一点。

我成功地用普通的引导输入标签和 Javascript 实现了这个目标,但不知何故我无法在此代码中做到这一点。

<ul id="check-list-box" class="list-group checked-list-box">
    <li class="list-group-item">Permutation</li>
    <li class="list-group-item">Combination</li>
    <li class="list-group-item">Probability</li>                                   
</ul>
 <button class="btn btn-primary" id="get-checked-data" onclick="myfunction()" >Get Checked Data</button>
 <button class="btn btn-primary" id="reset" name="reset_all" >Reset</button>

这是我用javascript写的:

function myfunction()
{
    if(document.getElementById('check-list-box').checked)
    {
        alert("hello-world");    
    }
    else
    {
        alert("not checked");
    }
}
//And something similar for resetting. But I am sure, I am missing something here

一些帮助将不胜感激。

这是一个使用引导程序的屏蔽 HTML,它看起来像一个复选框,但它不是一个复选框。 所以你的代码永远不会工作。

你必须以这种方式工作

if($(".checked-list-box li.list-group-item-primary.active").length > 0){
   alert("hello");
} else {
   alert("not checked");
}

我在上面的代码中写的是,当您选中上述 html 中的复选框时,li 添加了两个类"列表-组-项目-主要活动"

因此,最好将这些类放在您的 li 中。 这将为您提供复选框是否被选中。