douchao0358 2015-09-01 12:44
浏览 26
已采纳

JQUERY代码不适用于每个数组值

I want to do enable and disable select option while click on check box but it only work for single value not for each value of array.

If I used the id as array to get the values of select it doesn't work

Kindly update my code or provide me alternative way.I already try java script it also not working properly

 <script>

    var update_courier = function () {
        if ($("#checkbox").is(":checked")) {
            $('#courier').prop('disabled', false);
        }
        else {
            $('#courier').prop('disabled', 'disabled');
        }
    };

    $(update_courier);
    $("#checkbox").change(update_courier);
    </script>
    <div class="conatiner">
        <div class="col-sm-3 col-sm-offset-3" style="margin-top: -35%;">
        <form action="<?php echo site_url('invoice/invoidlist');  ?>" method="POST">
        <table class="table table-bordered">
        <?php foreach($details as $detail):?>

            <tr>
            <td><input id="checkbox" type="checkbox"  value="<?php echo $detail['OrderId'];?>"  name="checkbox">

                <td>
                    <?= $detail['OrderId']?>
                    </td>
                <td>
                    <?= $detail['CustomerName']?>
                    </td>
                    <td>
                    <?=$detail['Address']?>
                    </td>
                    <td>
                    <?=$detail['ZipCode']?>
                    </td>
                    <td>
                    <?=$detail['FinanicalStatus']?>
                    </td>
                    <td>
                    <?=$detail['Payment']?>
                    </td>
                <td>
                <select id="courier">
                <option>(choose one)</option>
        <option value='1'>Bluedart</option>
            <option value='2'>Delhivery</option>
            <option value='3'>DTDC</option>
            <option value='4'>IndiaPost</option>
    </select>

            </td>
                </tr>

            <?php endforeach; ?>
        </table>

    </div>
    <input type="submit" name="submit" value="submit" style="margin-left: 30%;">
    </form>
    </div>
    </div>

  • 写回答

1条回答 默认 最新

  • doudonglu3764 2015-09-01 13:02
    关注

    DEMO - Created static table in demo given here

    Instead of id on checkbox use class as id should be unique in HTML DOM and then you can write a click event to checkbox and enable or disable select element corresponding to its row as below

    $(".chkSelect").on('click',function(){
       $(this).closest('tr').find('.courier').prop('disabled',$(this).is(":checked"))
    });
    

    Here .chkSelect is class for checkbox and .courier is class for select

    Your updated php for table would be as below:

    <table class="table table-bordered">
         <?php foreach($details as $detail):?>
             <tr>
                <td>
                    <input class="chkSelect" type="checkbox"  value="<?php echo $detail['OrderId'];?>"  name="checkbox">
                <td>
                    <?= $detail['OrderId']?>
                </td>
                <td>
                    <?= $detail['CustomerName']?>
                </td>
                <td>
                    <?=$detail['Address']?>
                </td>
                <td>
                    <?=$detail['ZipCode']?>
                </td>
                <td>
                    <?=$detail['FinanicalStatus']?>
                </td>
                <td>
                    <?=$detail['Payment']?>
                </td>
                <td>
                    <select class="courier"> <!--Change this to class-->
                        <option>(choose one)</option>
                        <option value='1'>Bluedart</option>
                        <option value='2'>Delhivery</option>
                        <option value='3'>DTDC</option>
                        <option value='4'>IndiaPost</option>
                    </select>
                </td>
            </tr>
       <?php endforeach; ?>
    </table>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。