使用Code Igniter中的jQuery向DropDownList添加选项

时间:2010-09-27 11:12:09

标签: php javascript jquery codeigniter

我正在寻找一种方法,使用Jquery将自定义选项添加到下拉列表中。因此用户可以点击下拉列表,并显示一个选项列表,最后一个是“添加其他...”,一旦他们点击这个,他们就可以输入他们自己的选项(通过弹出,弹出,直接内联或然而。)

下面的帖子解释了它,但是,我正在使用CI作为数组等选项。如何在Code Igniter中应用它?

How do I add options to a DropDownList using jQuery?

谢谢!

P.S。我的知识不是很好。

1 个答案:

答案 0 :(得分:1)

尝试使用此尺寸:

http://jsfiddle.net/Fh5Bz/1/

<强> HTML

<select id="myselect">
    <option>hello</option>
    <option>world</option>
    <option>Add other...</option>
</select>

<div id="addother">
    <input type="text" id="addother_input" />
</div>​

<强> CSS

#addother {
    display:none;
}​

<强> JS

$(document).ready(function() {
    $('#myselect').change(function(e) {
        if ($(this).val() == "Add other...") {
            $('#addother').show();

            //set the input back to an empty string
            $('#addother_input').val('');
        } else {
            $('#addother').hide();
        }
    });
});​

编辑:很抱歉,错过了关于它是CI生成的select元素的界限。在CI中,您可以创建一个这样的select元素:

$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );

echo form_dropdown('shirts', $options);

如果您想创建一个额外的选项,只需将其添加到最后:

$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                  'addother' => "Add other..."
                );

echo form_dropdown('shirts', $options, null, 'id="myselect"');

然后,在你的jQuery中,测试$(this).val()==“addother”而不是“Add other ...”。