我有这段代码:
<select id="frm_type" name="typeId">
<?php foreach($product_types as $product) : ?>
<?php if ($product['id'] == $product_details['typeId']) $selected = " SELECTED"; else $selected = ""; ?>
<option value="<?=$product['id'];?>"<?=$selected;?>><?=$product['partNumber'];?> (<?=$product['title'];?>)</option>
<?php endforeach; ?>
</select>
这只会用数据库中的数据填充下拉列表/选项框。这在用于选择和发布项目的创建表单中工作正常,但是我希望在编辑页面上使用相同的功能。
编辑页面显示当前选定的值,如果未更改,则在发布时不会发布此输入中的任何内容。
我相信我用过的位$selected...在上面,显示选定的选项(它确实如此),它只是不发送选定的数据。
谁能提供任何建议。
尝试
<select id="frm_type" name="typeId">
<?php foreach($product_types as $product) :
$selected = ($product['id'] === $product_details['typeId'])
? "selected='selected'"
: "";
?>
<option value="<?=$product['id']?>" <?=$selected?> >
<?=$product['partNumber']?> (<?=$product['title']?>)
</option>
<?php endforeach; ?>
</select>
尝试
<select id="frm_type" name="typeId" disabled="disabled">
<?php foreach($product_types as $product) : ?>
<?php if ($product['id'] == $product_details['typeId']) $selected = " selected='selected'"; else $selected = ""; ?>
<option value="<?=$product['id'];?>"<?=$selected;?>><?=$product['partNumber'];?> (<?=$product['title'];?>)</option>
<?php endforeach; ?>
</select>