【问题标题】:Codeigniter - Form helper set_select()Codeigniter - 表单助手 set_select()
【发布时间】:2011-12-24 08:50:44
【问题描述】:

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

【问题讨论】:

    标签: php codeigniter codeigniter-form-helper


    【解决方案1】:

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

    虽然你可以这样做

    $selected = ($this->input->post('guide')) ? $this->input->post('guide') : 'investments';                        
    echo form_dropdown('guide', $guide_options, $selected);
    

    【讨论】:

      【解决方案2】:

      试试这个:

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

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

      【讨论】:

        【解决方案3】:

        您不必使用 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>
        

        【讨论】:

          【解决方案4】:

          这是一个 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 ':''?>
          

          【讨论】:

            【解决方案5】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-10-05
              相关资源
              最近更新 更多