使用单选按钮通过 Foreach 循环禁用下拉菜单

我正在开发一个应用程序/表单,它有一个表,该表通过带有单选按钮的 foreach 循环从数据库中填充,一个表示是,一个表示否,还有一个下拉列表。我需要它以便在选择“是”单选按钮时禁用 dd,在选择“否”时启用它。我让它工作,但它只适用于顶行,而且我无法预选所有的是单选按钮。所以简而言之,我无法弄清楚如何预选所有 yes rd btns,这反过来会禁用 dd。任何帮助都会很棒。谢谢。


<?php

            //Foreach loop iterates through each column of $getallrows function

            foreach($allRows as $rowID => $rowInfo){ ?>

              <tr>

                <td><?php echo $rowInfo['fpID'];?></td>

                <td><?php echo $rowInfo['shortTitle'];?></td>

                <td><?php echo $rowInfo['PI'];?></td>

                <td><?php echo $rowInfo['Department'];?></td>

                <td><?php echo $rowInfo['Division'];?></td>

                <td><?php echo $rowInfo['sponsorName'];?></td>

                <td><?php echo $rowInfo['Date_Project_Start']->format('Y-m-d');?></td>

                <td><?php echo $rowInfo['Date_Project_End']->format('Y-m-d');?></td>

                <td><?php echo $rowInfo['fundingType'];?></td>

                //Radio buttons

                <td>Yes<input type="radio" name="rdGrant" value="Yes"  id="rdYes" onclick="disable()" checked="checked"/><br />

                    No<input type="radio" name="rdGrant" value="No" id="rdNo" onclick="enable()"/></td>

                <form action="classes/functions.class.php" method="POST">

                  <input type="hidden" name="id" value="<?php echo $fpID; ?>"/>

                  <td>

                  //Drop down

                    <div class="dropdown">

                      <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" name="ddgrantGroup" id="ddgrantType" data-toggle="dropdown">Select Proper Funding Type


qq_遁去的一_1
浏览 85回答 1
1回答

精慕HU

您必须为按钮使用动态选择器(类/id)而不是常量 id #ddgrantType。尝试这样的事情 -&nbsp; &nbsp; &nbsp; &nbsp; <?php $dynamic_id = 'ddgrantType_'.$rowInfo['fpID'];?>&nbsp; &nbsp; &nbsp; &nbsp; <td>Yes<input type="radio" name="rdGrant" value="Yes"&nbsp; id="rdYes" onclick="disable('<?php echo $dynamic_id;?>')" checked="checked"/><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; No<input type="radio" name="rdGrant" value="No" id="rdNo" onclick="enable('<?php echo $dynamic_id;?>')"/></td>&nbsp; &nbsp; &nbsp; &nbsp; <form action="classes/functions.class.php" method="POST">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" name="id" value="<?php echo $fpID; ?>"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Drop down&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="dropdown">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" name="ddgrantGroup" id="<?php echo $dynamic_id;?>" data-toggle="dropdown">Select Proper Funding Type&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="caret"></span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ul class="dropdown-menu" aria-labelledby="ddgrantType">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li><a data-value="Corporate Sponsor">Corporate Sponsor</a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li><a data-value="Federal">Federal</a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li><a data-value="Foundation Selected">Foundation Selected</a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li><a data-value="Internally Funded">Internally Funded</a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li><a data-value="State/Local">State/Local</a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; <script>&nbsp; &nbsp; function disable(btn_id) {&nbsp; &nbsp; &nbsp; document.getElementById(btn_id).disabled=true;&nbsp; &nbsp; }&nbsp; &nbsp; function enable(btn_id) {&nbsp; &nbsp; &nbsp; document.getElementById(btn_id).disabled=false;&nbsp; &nbsp; }&nbsp; </script>&nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP