我有这个表格,有大约9个下拉菜单。我想在提交后保留选定的值。我的代码适用于其中2个未禁用的下拉菜单,但不适用于剩余的7个禁用菜单。是否有不同的方法为这些代码编写代码?
未被禁用的2个下拉中的1个: -
<?php
$sql = "SELECT * FROM tablename1 ";
$result = mysqli_query($conn, $sql);
echo "<select name = 'account_name' id = 'idaname'>";
echo "<option value = ''>";
while($row = mysqli_fetch_array($result)){
$selected = (isset($_POST['account_name']) && $_POST['account_name'] == $row['account_name']) ? 'selected = "selected"' :'';
?>
<option <?php echo $selected; ?> value = "<?php echo $row['account_name']; ?>"> <?php echo $row['account_name']; ?> </option>
<?php } ?>
</select>
7个禁用下拉中的2个: -
<?php
$sql = "SELECT rsm_val FROM tablename2 ";
$result = mysqli_query($conn, $sql);
echo "<select name = 'rsm_val' id = 'rsm_val' onchange = 'runrsm()' disabled >";
echo "<option value = '" .$rsm_val . "'>" . $rsm_val . "</option>";
while($row = mysqli_fetch_array($result)){
$selected = (isset($_POST['rsm_val']) && $_POST['rsm_val'] == $row['rsm_val']) ? 'selected = "selected"' :'';
?>
<option <?php echo $selected; ?> value = "<?php echo $row['rsm_val']; ?>"> <?php echo $row['rsm_val']; ?> </option>
<?php } ?>
</select>
<select name = 'boost_app' id = 'boost_app' onchange = 'runboost()' disabled>
<?php echo "<option value = '". $boost_app . "'>" . $boost_app . "</option>"; ?>
<option <?php if($_POST['boost_app'] == 'Yes'){?> selected="true" <?php }; ?> value = "Yes">Yes</option>
<option <?php if($_POST['boost_app'] == 'No') {?> selected="true" <?php }; ?> value = "No">No</option>
</select>
答案 0 :(得分:0)
您可以改为使用readonly
:
<select name = 'boost_app' id = 'boost_app' onchange = 'runboost()' readonly>
<?php echo "<option value = '". $boost_app . "'>" . $boost_app . "</option>"; ?>
<option <?php if($_POST['boost_app'] == 'Yes'){?> selected="true" <?php }; ?> value = "Yes">Yes</option>
<option <?php if($_POST['boost_app'] == 'No') {?> selected="true" <?php }; ?> value = "No">No</option>
</select>
这不允许用户编辑值,也会通过表单提交发送数据。 Learn more about it
修改如果readonly
不适用于<select>
代码,那么您可以disable
除所选内容之外的所有选项1.赞:< / p>
<select name = 'boost_app' id = 'boost_app' onchange = 'runboost()'>
<?php echo "<option value = '". $boost_app . "' disabled>" . $boost_app . "</option>"; ?>
<option <?php if($_POST['boost_app'] == 'Yes'){?> selected="true" <?php }else{echo "disabled";} ?> value = "Yes">Yes</option>
<option <?php if($_POST['boost_app'] == 'No') {?> selected="true" <?php }else{echo "disabled";} ?> value = "No">No</option>
</select>
或者您可以使用相同名称的hidden
输入,例如:
<input type="hidden" name="boost_app" value="<?=$_POST['boost_app']?>">
<select name = 'boost_app' id = 'boost_app' onchange = 'runboost()' disabled>
<?php echo "<option value = '". $boost_app . "'>" . $boost_app . "</option>"; ?>
<option <?php if($_POST['boost_app'] == 'Yes'){?> selected="true" <?php }; ?> value = "Yes">Yes</option>
<option <?php if($_POST['boost_app'] == 'No') {?> selected="true" <?php }; ?> value = "No">No</option>
</select>
答案 1 :(得分:0)
如果禁用下拉列表,则该值不会发送到服务器。您可以添加具有相同名称和值的隐藏输入。
<input type="hidden" name="boost_app" value="<?php echo $_POST['boost_app'];?>">
<select name = 'boost_app' id = 'boost_app' onchange = 'runboost()' disabled>
<?php echo "<option value = '". $boost_app . "'>" . $boost_app . "</option>"; ?>
<option <?php if($_POST['boost_app'] == 'Yes'){?> selected="true" <?php }; ?> value = "Yes">Yes</option>
<option <?php if($_POST['boost_app'] == 'No') {?> selected="true" <?php }; ?> value = "No">No</option>
</select>
答案 2 :(得分:0)
我使用它来使下拉菜单显示为“已禁用”
$(#boost_app).css({“ background-color”:“ rgb(238,238,238)”,“ pointer-events”:“ none”})。keydown(false)