4

如何在 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

4

5 回答 5

12

set_select如果您使用 html 代码而不是 helper 生成,则可以使用。

虽然你可以做这样的事情

$selected = ($this->input->post('guide')) ? $this->input->post('guide') : 'investments';                        
echo form_dropdown('guide', $guide_options, $selected);
于 2011-11-08T11:21:19.763 回答
3

尝试这个:

echo form_dropdown('guide', $guide_options,set_value('guide', 'investments'));

http://codeigniter.com/forums/viewthread/97665/

于 2011-11-08T11:19:40.117 回答
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>
于 2011-11-08T11:16:37.207 回答
1

Here's a working example of set_select()

<?php echo set_select('my_select', $data['id'])?>

Full code:

<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>

Or you can use a pure PHP approach:

// Use this inside in a <option> generated by a foreach, like the example above.
<?php echo $id_city == $city['id']?' selected ':''?>
于 2017-03-15T20:05:29.493 回答
0

要使用 set_select,您需要为相关字段设置验证规则。在此处查看我对类似问题的回答: CodeIgniter select_value in form_dropdown

于 2014-12-14T23:05:55.600 回答