【问题标题】:add disabled (and selected) option to select item with form helper添加禁用(和选定)选项以使用表单助手选择项目
【发布时间】:2010-11-04 17:10:49
【问题描述】:

嗨 我正在尝试使用表单助手将禁用选项添加到选择框我使用此代码生成一个额外的空字段,但我希望禁用此字段。

echo $this->Form->input('User.usertype_id',array('type'=>'select', 'empty'=>'usertype');

这会生成:

<div class="input select">
    <label for="UserUsertypeId">Usertype</label>
    <select name="data[User][usertype_id]" id="UserUsertypeId">
        <option value="">usertype</option>
        <option value="1">athlete</option>
        <option value="2">trainer</option>
    </select>
</div>

但我想要这个:

<div class="input select">
    <label for="UserUsertypeId">Usertype</label>
    <select name="data[User][usertype_id]" id="UserUsertypeId">
        <option value="" disabled="disabled" selected="selected">usertype</option>
        <option value="1">athlete</option>
        <option value="2">trainer</option>
    </select>
</div>

有什么方法可以简单地做到这一点,还是我应该只使用一些js?

【问题讨论】:

    标签: cakephp formhelper


    【解决方案1】:

    如果您事先知道选项,则可以构建$options 数组以在选择菜单中使用。这应该给你你想要的:

    $options = array(
                array(
                    'name' => 'usertype',
                    'value' => '',
                    'disabled' => TRUE,
                    'selected' => TRUE
                ),
                'athlete',
                'trainer'
                );
    
    echo $this->Form->input('User.usertype_id', array('type' => 'select', 'options' => $options));
    

    或者这可能可行,但我也没有测试过:

    echo $this->Form->input('User.usertype_id', array('type' => 'select', 'empty'=> array('text' => 'usertype', '已选择' => TRUE, '已禁用' => FALSE)));

    【讨论】:

    • 工作正常.. 谢谢。
    【解决方案2】:

    我知道这个问题最后一次更新是在 2010 年,但我有实际答案。 查看 CakePHP 文档中的示例:

    $options = array(
        'Value 1' => 'Label 1',
        'Value 2' => 'Label 2'
    );
    echo $this->Form->select('Model.field', $options, array(
        'multiple' => 'checkbox',
        'disabled' => array('Value 1')
    ));
    

    http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::select

    【讨论】:

      【解决方案3】:

      试试这个!!

      $operations = [
        '' => 'Select Operation',
        'query' => 'Query',
        'create' => 'Create',
        'update' => 'Update',
        'upsert' => 'Upsert',
        'delete' => 'Delete'
      ];
      
      echo $this->Form->input('operation[]',[
        'type' => 'select',
        'options' => $operations,
        'class' => 'operation-class',
        'id' => 'operation-id-1',
        'required' => 'required',
        'label' => false,
        'disabled' => [''],
        'value' => ''
      ]);
      

      【讨论】:

        【解决方案4】:

        嗯,似乎无法在评论中添加一些代码块 因此,您的两个选项都生成了:

        <select name="data[User][usertype_id]" id="UserUsertypeId">
          <option value="text">usertype</option>
          <option value="selected">1</option>
          <option value="disabled"></option>
          <option value="1">athlete</option>
          <option value="2">trainer</option>
        </select>
        

        所以这不起作用,但我是这样做的:

        echo $this->Form->input('User.usertype_id', array('type' => 'select', 'empty'=> array('text" disabled="disabled" selected="selected' => '')));
        

        这会生成一个选项,其值为:(" disabled="disabled" selected="selected) 所以它变成了:

        ...
        <option value="" disabled="disabled" selected="selected"></option>
        ...
        

        这是一个临时解决方案,直到我找到更好的解决方案,欢迎提出建议!

        【讨论】:

        • 再次查看我的答案。我已经编辑了第一个选项,使其正常工作。
        【解决方案5】:

        我合并了 Weptunus 和 stevelove 的解决方案。

        在控制器中:

        $examples = $this->Test->Examples->find('list');
        $this->set('examples', $examples);
        

        在视图中

        echo $this->Form->input('example_id', array('empty' => array(
                '' => array(
                    'name' => __('Select an Example'),
                    'value' => '',
                    'disabled' => TRUE,
                    'selected' => TRUE
                )
            ))
        );
        

        【讨论】:

          【解决方案6】:

          我在 cake 1.3 中发现了这个成功

          <?php echo $this->Form->input('example', array('type' => 'select','id' => 'id','options' => array('empty "disabled="disabled" selected="selected"' => 'name empty', 'val1' => 'text1', 'val2' => 'text2')); ?>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2022-07-26
            • 1970-01-01
            • 2015-09-29
            • 2015-03-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多