简体   繁体   English

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

[英]How I get the text instead value from a select option in 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): 提交表单后,将通过controller(text.php)调用适当的模型:

text.php -> insert_text() text.php-> insert_text()

$this->admin_text_model->insert_text();

admin_text_model.php 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')? 有没有办法让我在$ this-> input-> post('pos')的选项标签中获取文本?

Perhaps something like: 也许像这样:

'tx_pos' => $this->input->post('pos')->text?

No, text inside <option> tags is never submitted. 不, <option>标记内的文本永远不会提交。 That is just how HTML works. 这就是HTML的工作方式。

You must set the values, that you need to retrieve, in the value attribute. 您必须在value属性中设置需要检索的value That is what they are for. 那就是他们的目的。

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. 它将返回选项文本。

  1. You could make the select yourself and not using the CI helper. 您可以自己选择,而不使用CI帮助器。 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: 或者 ,您可以使用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/ 它将每个选项的value设置为等于其文本内容: http : //jsfiddle.net/ursQY/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM