如何在codeigniter中的下拉列表中使用set_select()(表单助手的一部分):
用于记住用户选择的选项。
$guide_options = array('investments' => 'Investments',
'wills' => 'Wills',
'tax-planning' => 'Tax planning',
'life-insurance' => 'Life insurance',
'currency-exchange' => 'Currency exchange',
'retirement-planning' => 'Retirement planning',
'international-healthcare' => 'International healthcare',
'savings-plans' => 'Savings plans',
'education-planning' => 'Education planning',
'sipps' => 'SIPPS',
'qrops' => 'QROPS',
'qnups' => 'QNUPS',
'mortgages' => 'Mortgages',
'other' => 'Other service'
);
echo form_dropdown('guide', $guide_options, 'investments');
表格帮助指南:http://codeigniter.com/user_guide/helpers/form_helper.html
答案 0 :(得分:12)
set_select
如果您使用html代码生成而不是使用帮助程序,则可以使用。
虽然你可以做这样的事情
$selected = ($this->input->post('guide')) ? $this->input->post('guide') : 'investments';
echo form_dropdown('guide', $guide_options, $selected);
答案 1 :(得分:3)
试试这个:
echo form_dropdown('guide', $guide_options,set_value('guide', 'investments'));
答案 2 :(得分:1)
您不必使用set_select(),因为您已经在from_dropdown()中定义了所选项目
form_dropdown('guide', $guide_options, 'investments');
为了更好地理解,请参考下面的代码和输出
$options = array(
'small' => 'Small Shirt',
'med' => 'Medium Shirt',
'large' => 'Large Shirt',
'xlarge' => 'Extra Large Shirt',
);
$shirts_on_sale = array('small', 'large');
echo form_dropdown('shirts', $options, 'large');
// Would produce:
<select name="shirts">
<option value="small">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>
答案 3 :(得分:1)
这是set_select()
的一个工作示例<?php echo set_select('my_select', $data['id'])?>
完整代码:
<select class="form-control" required name="my_select">
<option value="">- Select -</option>
<?php foreach ($datas as $data): ?>
<option value="<?php echo $data['id']?>" <?php echo set_select('my_select', $data['id'])?>><?php echo $data['name']?></option>
<?php endforeach; ?>
</select>
或者您可以使用纯PHP方法:
// Use this inside in a <option> generated by a foreach, like the example above.
<?php echo $id_city == $city['id']?' selected ':''?>
答案 4 :(得分:0)
要使用set_select,您需要为相关字段设置验证规则。在这里查看我对类似问题的回答: CodeIgniter select_value in form_dropdown