2017-10-06 73 views
0

你好我试图检查所有复选框在我的表中的每个row当我Dropdown menuSentStatus Column的值等于Sent。我现在可以选中/取消选中我桌子上的所有复选框。但现在我需要检查所有这些状态是Sent,当下拉菜单也是Sent。我赞赏所有答案。请检查我的代码如下。非常感谢如何检查具有相同值的所有复选框,从另一列

view.html

    <thead> 
        <tr> 
         <td><strong><input type="checkbox" id="checkAll">Check All</strong></td> 
         <td><strong>Status</strong></td> 

        </tr> 
        </thead> 

       <tbody> <tr> 
        <td><input type="checkbox"/></td> 
        <td value="Sent">Sent</td> 
       </tr> 
       <tr> 
        <td><input type="checkbox" /></td> 
        <td value="Created">Created</td> 
       </tr> 
       <tr> 
        <td><input type="checkbox"/></td> 
        <td value="Created">Created</td> 
       </tr> 
       <tr> 
        <td><input type="checkbox" /></td> 
        <td value="Sent">Sent</td> 
       </tr> 
       <tr> 
        <td><input type="checkbox" /></td> 
        <td value="Sent">Sent</td> 
       </tr> </tbody> 

       <select class="form-control" id="default-select"> 

       <option value="delete">Created</option> 
       <option value="stored">Stored Status</option> 
       <option value="sent">Sent Status</option> 
            </select> 

的script.js

$("#checkAll").click(function(){ 
    if($('#default-select option:selected').val() == 'sent'){ 
    $('#testTable tbody input[type="checkbox"]').prop('checked', this.checked); 
}}); 
+0

尝试更换'this.checked'用'$(本)。是( “:勾选”)' – diavolic

回答

1

DEMO附:更新请求后,这可能帮助

$(document).ready(function(){ 
 
    $("#default-select").on("change",function(){ 
 
    $("input").prop("checked",false); 
 
     
 
$("td").each(function(i,v){ 
 
    
 
if($(this).text().trim() == $("#default-select").val()){ 
 
    
 
     $(this).closest("tr").find("input").prop("checked",true); 
 
     } 
 
}); 
 

 
}); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 

 
<table> 
 
<thead> 
 
        <tr> 
 
         <td><strong><input type="checkbox" id="checkAll">Check All</strong></td> 
 
         <td><strong>Status</strong></td> 
 

 
        </tr> 
 
        </thead> 
 

 
       <tbody> 
 
       <tr> 
 
        <td><input type="checkbox" /></td> 
 
        <td>sent</td> 
 
       </tr> 
 
       <tr> 
 
        <td><input type="checkbox" /></td> 
 
        <td >created</td> 
 
       </tr> 
 
       <tr> 
 
        <td><input type="checkbox"/></td> 
 
        <td >created</td> 
 
       </tr> 
 
       <tr> 
 
        <td><input type="checkbox" /></td> 
 
        <td>sent</td> 
 
       </tr> 
 
       <tr> 
 
        <td><input type="checkbox" /></td> 
 
        <td>sent</td> 
 
       </tr> 
 
       <tr> 
 
        <td><input type="checkbox"/></td> 
 
        <td>stored</td> 
 
       </tr> 
 
       </tbody> 
 
</table> 
 
<select class="form-control" id="default-select"> 
 

 
       <option value="created">Created</option> 
 
       <option value="stored">Stored Status</option> 
 
       <option value="sent">Sent Status</option> 
 
            </select>

+0

真棒!但我怎么能通过获得复选框的类,而不是'价值'? – Dre

+0

为此,您可以使用$(this).attr(“class”)而不是$(this).val(),如果这有帮助,请将答案标记为接受。 –

+0

对不起,但如何通过不将复选框'value'设置为'sent'来执行此操作?因为我的复选框已经有一个'value' – Dre

1

这是我喜欢做的simpliest方式。检查出DEMO。我给clas复选框,并在更改select的值时触发该函数,然后检查值是什么。如果它被发送,然后点击我创建的类。

+0

我尝试了你的,但它不起作用 – Dre

+0

它工作,但只适用于“发送”。如果你想添加更多的过滤器,而不是你需要定义更多的var到函数中,并检查var'if else',我相信你有足够的知识来做到这一点。 –

相关问题