php 在 codeigniter form_dropdown 中添加禁用选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6797576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:17:07  来源:igfitidea点击:

Adding disabled option in codeigniter form_dropdown

phpcodeigniterdrop-down-menu

提问by beholder

I'm trying to figure out how to add a disabled option in my dropdown, using codeIgniter. New to CI, and I've tried googling it a bit, but couldn't find an answer.

我试图弄清楚如何使用 codeIgniter 在我的下拉列表中添加禁用选项。CI 的新手,我试过在谷歌上搜索一下,但找不到答案。

My code for a dropdown looks like this:

我的下拉代码如下所示:

    echo form_dropdown('category', array('0' => 'Choose a category...')  + $categories, '0');

This gives me a dropdown with all my options from the variable $categories, with "Choose a category..." (value 0) at top. Now how to I make the first one disabled? I know how to make it select a specific one, which I've set it to do here.

这给了我一个下拉列表,其中包含变量 $categories 中的所有选项,顶部有“选择一个类别...”(值 0)。现在如何禁用第一个?我知道如何让它选择一个特定的,我在这里设置了它。

Can anyone help me? Thanks

谁能帮我?谢谢

回答by bubjavier

I know this is an old post, but in the current version of CI, I can do a little (sql-injection-like) trick by appending " disabled="disabledto the keys of the option I would like to disable.

我知道这是一篇旧帖子,但在当前版本的 CI 中,我可以通过附加" disabled="disabled到我想禁用的选项的键来做一些(类似于 sql 注入)的技巧。

$categories['0'] = '(Select Category)';
$categories['1'] = 'Category 1';
$categories['2" disabled="disabled'] = 'Restricted Category';
$categories['3'] = 'Category 3';

echo form_dropdown('category', $categories, '0');

I am not sure if this is a bug of CI's form_helper, since it does not do any escaping or sanitizing function for the dropdown key/values. For the mean time, to be safe, just make sure your keys and values wont be coming from any user-based input.

我不确定这是否是 CI 的 form_helper 的错误,因为它不会对下拉键/值执行任何转义或清理功能。同时,为了安全起见,只需确保您的键和值不会来自任何基于用户的输入。

回答by gazdagergo

If you have a few static options only the $options variable can be a simple string too, containing the options in html format, like this:

如果您有几个静态选项,则 $options 变量也可以是一个简单的字符串,包含 html 格式的选项,如下所示:

$options = "
<option value=0 disabled>Select Category</option>
<option value=1>Category 1</option>
<option value=2>Category 2</option>";

echo form_dropdown('category', $options, '0');

回答by Vipin Kr. Singh

Just add the fourth param $extra to your dropdown as a string like this 'disabled=disabled' as explained in CI Docs at https://codeigniter.com/user_guide/helpers/form_helper.html#available-functions

只需将第四个参数 $extra 作为这样的字符串添加到您的下拉列表中,如https://codeigniter.com/user_guide/helpers/form_helper.html#available-functions中 CI Docs 中所述

echo form_dropdown('category', array('0' => 'Choose a category...')  + $categories, '0', 'disabled=disabled');

回答by toopay

If you still want to use form_helper, you always can extends helper file and make 'disable' tag available. Create MY_Form_helper.php and put that under helper directory, then define function form_dropdown in that custom helper, then it will overide the form helper behaviour.

如果你仍然想使用 form_helper,你总是可以扩展帮助文件并使“禁用”标签可用。创建 MY_Form_helper.php 并将其放在 helper 目录下,然后在该自定义助手中定义函数 form_dropdown,然后它将覆盖表单助手的行为。

回答by Whole Hat

<?php echo validation_errors(); ?>
<?php echo form_open(''); ?>

<?php echo form_label('Gender:'); ?>
<?php echo form_dropdown(array('id'=>'selectinform', 'name'=>'gender', 'options'=>array('1'=>'Select', '2'=>'2','3'=>'3'), 'selected'=>'1')); ?>

<?php echo form_submit(array('id'=>'submit', 'value'=>'submit')); ?>
<?php echo form_close(); ?>

<script>
    $(document).ready(function(){
        $("#selectinform option:first").attr('disabled', 'disabled');
    });
</script>

Result - IMG

结果 - IMG

回答by sqwk

From the CI user guide at http://codeigniter.com/user_guide/helpers/form_helper.html:

来自http://codeigniter.com/user_guide/helpers/form_helper.html的 CI 用户指南:

If you would like the opening to contain additional data, like an id attribute or JavaScript, you can pass it as a string in the fourth parameter:

如果您希望开头包含其他数据,例如 id 属性或 JavaScript,您可以将其作为字符串传递到第四个参数中:

So you code becomes:

所以你的代码变成:

    echo form_dropdown('category', array('0' => 'Choose a category...')  + $categories, '0', 'disabled="disabled"');

However, unless you are a PHP fanatic or are using CSRF protection through the form helper I would just type out your form html yourself. It is the same amount of text and you are using less functions.

但是,除非您是 PHP 狂热者或通过表单助手使用 CSRF 保护,否则我只会自己输入您的表单 html。它是相同数量的文本,您使用的功能较少。

回答by igolka97

Solution:

解决方案:

Change the 437th string of form_helper.php:

更改 的第 437 个字符串form_helper.php

from

.(in_array($key, $selected) ? ' selected="selected"' : '').'>'

to

.(in_array($key, $selected) ? ' disabled="disabled"' : '').'>'

and use selectedfeature as disabled. Looks like an lifehack, but it works

并将selected特征用作disabled. 看起来像一个救生员,但它有效