简体   繁体   English

根据下拉值启用/禁用复选框

[英]Enable/Disable checkbox based on value of dropdown

I have an HTML table that includes a dropdown with 3 selections, Yes, No, Exempt and also a checkbox. 我有一个HTML表格,其中包含一个包含3个选择的下拉菜单,是,否,免除以及一个复选框。 I have set the value of Yes = 1, No = 0, and Exempt = 2. On page load, if the dropdown value equals 0 or 2, then the checkbox should be disabled. 我已将值设置为Yes = 1,No = 0,并且Exempt =2。在页面加载时,如果下拉值等于0或2,则应禁用该复选框。 If the dropdown value equals 1, then the checkbox should be enabled. 如果下拉值等于1,则应启用该复选框。 However, currently I have had no luck with getting the correct result. 但是,目前我并没有获得正确的结果。 I was able to get the value of the first row before, but that did not traverse through the entire table. 我能够获得之前第一行的值,但是并没有遍历整个表。

How can I traverse through the entire table to find each value of the dropdown, and also enable/disable the checkbox based on what is in the dropdown? 如何遍历整个表以查找下拉列表的每个值,以及如何基于下拉列表中的内容启用/禁用复选框?

Javascript: Javascript:

$(document).ready(function () {
    $('#billing_table tr').each(function() {

    var checked = $(this).find($(".billed-select")).val();
    console.log(checked);

    if(checked != "1") {
        $('.paid').attr('disabled', true);
    } else {
        $('.paid').attr('disabled', false);
    }
  });
});

HTML Table: HTML表格:

<table id="billing_table">
<thead>
    <tr>
    <td style="display: none">Account</td>
    <td>Collector Name</td>
    <td>Customer</td>
    <td>Bill Date</td>
    <td>Days Until<br>Next Action</td>
    <td>Next Action</td>
    <td>Billed</td>
    <td>Bill<br>Amount</td>
    <td>Payment<br>Due Date</td>
    <td>Notes</td>
    <td>Paid</td>
    <td>Save</td>
    </tr>
</thead>
<tbody>


<?php
    foreach ($dbh->query($bill) as $rows) {
    ?>
    <tr class="row">
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>   

        <?php if ($rows ['Billed Status'] == 'Billed') {?>      
        <td>
            <select class="billed-select">
                <option class="selection" value="1" selected>Yes</option>
                <option class="selection" value="0">No</option>
                <option class="selection" value="2">Exempt</option>
            </select>
        </td>    
        <?php } else if ($rows ['Billed Status'] == 'Unbilled'){ ?>       
        <td>
            <select class="billed-select">
                <option class="selection" value="1">Yes</option>
                <option class="selection" value="0" selected>No</option>
                <option class="selection" value="2">Exempt</option>
            </select>
        </td>
        <?php } else if ($rows ['Billed Status'] == 'Exempt'){ ?>       
        <td>
            <select class="billed-select">
                <option class="selection" value="1">Yes</option>
                <option class="selection" value="0">No</option>
                <option class="selection" value="2" selected>Exempt</option>
            </select>
        </td>
        <?php } ?>

        <td></td>
        <td></td>
        <td></td>

        <?php if ($rows ['Payment Status'] == 'Paid') {?>
        <td align="center"><input type="checkbox" id="paid" class="paid" name="paid" checked></td>
        <?php } else { ?>
        <td align="center"><input type="checkbox" id="paid" class="paid" name="paid"></td>
        <?php } ?>

        <td></td>
    </tr>
 <?php
  }
 ?>
</tbody>
</table>

Modified the php out to show a selected value that WOULD make it disabled. 修改了php,以显示将使其禁用的选定值。

 $(document).ready(function() { // add row class, avoid headers $('#billing_table tr.row').each(function() { var checked = $(this).find(".billed-select").find("option:selected").val(); console.log("checked:",checked);// alerts 0 $(this).find('.paid').prop('disabled', (checked != "1")); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table id="billing_table"> <thead> <tr> <td style="display: none">Account</td> <td>Collector Name</td> <td>Customer</td> <td>Bill Date</td> <td>Days Until<br>Next Action</td> <td>Next Action</td> <td>Billed</td> <td>Bill<br>Amount</td> <td>Payment<br>Due Date</td> <td>Notes</td> <td>Paid</td> <td>Save</td> </tr> </thead> <tbody> <tr class="row"> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td> <select class="billed-select"> <option class="selection" value="1" >Yes</option> <option class="selection" value="0" selected>No</option> <option class="selection" value="2">Exempt</option> </select> </td> <td></td> <td></td> <td></td> <td align="center"><input type="checkbox" id="paid" class="paid" name="paid" checked></td> <td></td> </tr> </tbody> </table> 

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

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