使用单选按钮使用Foreach循环禁用下拉菜单
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用单选按钮使用Foreach循环禁用下拉菜单相关的知识,希望对你有一定的参考价值。
我正在使用一个应用程序/表单,该应用程序/表单具有一个通过foreach循环从数据库中填充的表,该表带有单选按钮,一个表示是,一个表示否,以及一个下拉列表。我需要它,以便在选择“是”单选按钮时禁用dd,而在选择“否”时启用它。我可以使用它,但是它仅适用于第一行,而且我无法预先选择所有“是”单选按钮。简而言之,我无法弄清楚如何预先选择所有yes rdns 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
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="ddgrantType">
<li><a data-value="Corporate Sponsor">Corporate Sponsor</a></li>
<li><a data-value="Federal">Federal</a></li>
<li><a data-value="Foundation Selected">Foundation Selected</a></li>
<li><a data-value="Internally Funded">Internally Funded</a></li>
<li><a data-value="State/Local">State/Local</a></li>
</ul>
</div>
</td>
<td>
<div class="update">
<!-- <a href="index.php?edit=<?php echo $rowInfo['fpID']; ?>" class="btn btn-success">Update and Save</a> -->
<button type="submit" class="btn btn-success" name="Update">Update and Save</button>
</div>
</td>
<td>
<div class="comment">
<textarea class="form-control" aria-label="With textarea" id="grantComment" placeholder="Comments"></textarea>
<button type="submit" class="btn btn-success" name="updateComm">Add Comments</button>
<!-- <a href="index.php?edit<?php echo $rowInfo['fpID']; ?>" class="btn btn-success">Add Comment</a> -->
</div>
</td>
</form>
</tr>
<?php } ?>
</tbody>
单选按钮后面的功能禁用/启用dd。
function disable() {
document.getElementById("ddgrantType").disabled=true;
} function enable() {
document.getElementById("ddgrantType").disabled=false;
}
答案
您必须为按钮使用动态选择器(类/ id),而不是常量ID #ddgrantType。
尝试这样的事情-
<?php $dynamic_id = 'ddgrantType_'.$rowInfo['fpID'];?>
<td>Yes<input type="radio" name="rdGrant" value="Yes" id="rdYes" onclick="disable('<?php echo $dynamic_id;?>')" checked="checked"/><br />
No<input type="radio" name="rdGrant" value="No" id="rdNo" onclick="enable('<?php echo $dynamic_id;?>')"/></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="<?php echo $dynamic_id;?>" data-toggle="dropdown">Select Proper Funding Type
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="ddgrantType">
<li><a data-value="Corporate Sponsor">Corporate Sponsor</a></li>
<li><a data-value="Federal">Federal</a></li>
<li><a data-value="Foundation Selected">Foundation Selected</a></li>
<li><a data-value="Internally Funded">Internally Funded</a></li>
<li><a data-value="State/Local">State/Local</a></li>
</ul>
</div>
</td>
<script>
function disable(btn_id) {
document.getElementById(btn_id).disabled=true;
}
function enable(btn_id) {
document.getElementById(btn_id).disabled=false;
}
</script>
以上是关于使用单选按钮使用Foreach循环禁用下拉菜单的主要内容,如果未能解决你的问题,请参考以下文章