英文:
Set a default value to a Combobox in php
问题
我修改了一个代码,该代码在下拉框中提供了一组值。但是,我想在下拉框显示的同时将默认值设置为下拉框中的一个选项。这是我的代码。如果您能帮助我,我真的很感激您的帮助。提前感谢。
// 添加一个国家的下拉列表
<div class="col-sm-offset-2 col-xs-9">
<select class="select2" name="country" id="country" data-placeholder="Country" required style="width:100%;">
<option value="">Select</option>
<?php foreach($this->countries as $pro){
echo '<option value="' . $pro->id . '">' . $pro->country_name . '</option>';
}
?>
</select>
</div>
你可以通过修改 value
属性来设置默认值,例如,将 <option value="">Select</option>
中的 value
属性设置为所需的默认值。
英文:
I modify a code which gives a set of values as dropdown in a combobox. But I want to set a default value to the combobox while the drop down is there. This is my code. If you can help me I really appreciate your help. Thnks in advance.
//Add a drop down list of countries
<div class="col-sm-offset-2 col-xs-9">
<select class="select2" name="country" id="country" data-placeholder="Country" required style="width:100%;">
<option value=""> Select </option>
<?php foreach($this->countries as $pro){
echo '<option value="'.$pro->id.'">'.$pro->country_name.'</option>';
}
?>
</select>
</div>
答案1
得分: 1
添加此条件
<div class="col-sm-offset-2 col-xs-9">
<select class="select2" name="country" id="country" data-placeholder="Country" required style="width:100%;">
<option value="">选择</option>
<?php foreach($this->countries as $pro){
echo '<option value="'.$pro->id.'" '.if($pro->country_name == "印度"){.'selected'}.'>'.$pro->country_name.'</option>';
}
?>
</select>
</div>
英文:
Add this Condition
<div class="col-sm-offset-2 col-xs-9">
<select class="select2" name="country" id="country" data-placeholder="Country" required style="width:100%;">
<option value=""> Select </option>
<?php foreach($this->countries as $pro){
echo '<option value="'.$pro->id.'" '.if($pro->country_name == "india"){.'selected'.}.'>'.$pro->country_name.'</option>';
}
?>
</select>
</div>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论