我有这样的HTML:
<select id="job" name="job" class="form-control">
<option value="First Option">First Option</option>
<option value="Second Option">Second Option</option>
<option value="Third Option">Third Option</option>
</select>
我还有一个像这样的PHP变量:$val = "Second Option";
。现在我需要将selected
属性添加到与$val
变量具有相同值的选项中吗?
答案 0 :(得分:0)
你可以这样做:(最好在这里使用三元条件)
<select id="job" name="job" class="form-control">
<option value="First Option" <?php echo ($val == "First Option")? "selected":"";?>>First Option</option>
<option value="Second Option" <?php echo ($val == "Second Option")? "selected":"";?>>Second Option</option>
<option value="Third Option" <?php echo ($val == "third Option")? "selected":"";?>>Third Option</option>
</select>