简体   繁体   English

多复选框要求使用jQuery启用禁用下拉框

[英]Multi checkbox requirement enable disable dropdown box using jquery

So first off I did search and I found this Enable/Disable a dropdownbox in jquery which got me on the right track. 因此,首先我进行了搜索,然后在jquery中找到了“ 启用/禁用”下拉框 ,这使我处于正确的轨道。 I'm new to jquery so when seeing other code I can adapt it to fit and work for me. 我是jquery新手,因此在查看其他代码时,我可以对其进行调整以适合我并适合我。

So what i'm asking is how do you make it check to see if two check boxes condition are true? 所以我要问的是如何使它检查两个复选框的条件是否为真?

<script>
    $(document).ready(function() {
         $("#box1").click(function() {
           if ($(this).is(":checked")) {
              $("#tech1").prop("disabled", false);
           } else {
              $("#tech1").prop("disabled", true);  
           }
         });
        });
</script>

I want it to be if box1 and box2 are checked enable this box? 我希望如果选中box1和box2启用此框? I understand you can do it with an if statement, but I'm not sure where exactly it goes. 我了解您可以使用if语句来执行此操作,但是我不确定它的确切位置。 Thanks in advance. 提前致谢。

Would this work: 这项工作会:

$("#box1").click(function() {
    if ($(this).is(":checked")) &&
$("#box2").click(function() {
    if ($(this).is(":checked"))

I doesn't work and I assume thats because that above creates two functions and not the if statement that I need. 我不工作,我认为那是因为上面创建了两个函数,而不是我需要的if语句。

Include both in the event handler, and check if both are checked 将两者都包含在事件处理程序中,并检查是否都选中

$(document).ready(function() {
    var boxes = $("#box1, #box2");

    boxes.on('change',  function() {

       var disabled = boxes.filter(':checked').length === boxes.length;

       $("#tech1").prop("disabled", disabled);

    });
});

FIDDLE 小提琴

Consider box1 & box2 checkboxes defined with a css class boxes as below: 考虑使用CSS类定义的box1和box2复选框,如下所示:

<input type="checkbox" id="box1" class="boxes" />
<input type="checkbox" id="box2" class="boxes" />

Now you can use box class as jquery selector to do your task 现在,您可以将box类用作jquery选择器来执行任务

<script>
$(document).ready(function() {
    $(".boxes").click(function(){
         if($(".boxes:checked").size() == $(".boxes").size()){
            $("#tech1").prop("disabled", false);
         }else{
            $("#tech1").prop("disabled", true);  
         }
     );
});

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

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