dongzhang6677
dongzhang6677
2014-05-30 05:45
浏览 41
已采纳

我如何从CodeIgniter中的选择选项获取文本而不是值

I have a form in which there is a select field with many options. These options have numeric values and a text between option tags. What I want to do is to get the text instead of values when I post the form via submit button.

View that generates the select field:

echo form_dropdown('pos', $pos_options, '100', 'id="pos_select" class="form_input"'); 

the select part generated:

<select name="pos" id="pos_select" class="form_input">
    <option value="0">about_history_title</option>
    <option vlaue="1">about_history</option>
</select>

When the form is submited, the appropriate model is called via the controller(text.php):

text.php -> insert_text()

$this->admin_text_model->insert_text();

admin_text_model.php

public function insert_text()
{
    $data = array(
        'tx_page' => $this->input->post('page'),
        'tx_pos' => $this->input->post('pos'),
        'tx_body' => $this->input->post('add_text')
    );

    return $this->db->insert('text', $data);
}

Is there a way that I can get the text inside option tags in $this->input->post('pos')?

Perhaps something like:

'tx_pos' => $this->input->post('pos')->text?
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doupo2157
    doupo2157 2014-05-30 06:02
    已采纳

    Try this don't assign the value to option tag:

    <select name="pos" id="pos_select" class="form_input">
        <option>about_history_title</option>
        <option>about_history</option>
    </select>
    

    It will return text of option.

    点赞 评论
  • dswsl2016
    dswsl2016 2014-05-30 05:54

    No, text inside <option> tags is never submitted. That is just how HTML works.

    You must set the values, that you need to retrieve, in the value attribute. That is what they are for.

    点赞 评论
  • dsjpik057730
    dsjpik057730 2014-05-30 06:04
    1. You could make the select yourself and not using the CI helper. This way you have full control on what and where to output it:

      <select name="pos" id="pos_select" class="form_input">
        <?php foreach($pos_options as $option):?>
          <option value="<?php echo htmlentities($option);?>"><?php echo $option;?></option>
        <?php endforeach;?>
      </select>
      
    2. Or, you could make an ugly hack in javascript:

      $('#pos_select option').each(function(){
         $(this).attr('value', $(this).text()); 
      });
      

    Which sets the value of every option equal to its text content: http://jsfiddle.net/ursQY/

    点赞 评论

相关推荐