【问题标题】:How to set disabled select option in Laravel with Illuminate/Html如何在 Laravel 中使用 Illuminate/Html 设置禁用的选择选项
【发布时间】:2015-06-28 13:01:53
【问题描述】:

我从 Laravel 开始,我正在使用 Illuminate/Html 来制作表单。

我想在第一个选项中添加禁用属性,但我没有找到方法。

{!! Form::open(['url' => 'shelter/pets']) !!}
    <div class="form-group">
        {!! Form::label('pet_type','Type:') !!}
        {!! Form::select('pet_type', ['Select Type','dog', 'cat'], 0, ['class' => 'form-control']) !!}
    </div>
    <div class="form-group">
        {!! Form::submit('Add pet', null, ['class' => 'btn btn-primary form-control']) !!}
    </div>
{!! Form::close() !!}

【问题讨论】:

  • 嗨 javipedrera,你得到答案了吗?如果你找到答案,请帮助我。我面临同样的问题:)

标签: php forms laravel blade illuminate-container


【解决方案1】:

作为这里函数的签名: 供应商/laravelcollective/html/src/FormBuilder.php 第 625 行:

/**
     * Create a select box field.
     *
     * @param  string $name
     * @param  array  $list
     * @param  string|bool $selected
     * @param  array  $selectAttributes
     * @param  array  $optionsAttributes
     * @param  array  $optgroupsAttributes
     *
     * @return \Illuminate\Support\HtmlString
     */
    public function select(
        $name,
        $list = [],
        $selected = null,
        array $selectAttributes = [],
        array $optionsAttributes = [],
        array $optgroupsAttributes = []
    )

所以你可以像这样使用它:

{!! Form::select('pet_type', 
    ['Select Type','dog', 'cat'],
     0, //default selection
    ['class' => 'form-control'], //the select tag attributes
    [ 0 => [ "disabled" => true ] ] //list of option attrbitues (option value is the arrays key)
) !!}

【讨论】:

    【解决方案2】:

    这对你有帮助

    Form::select('name_select', '', null, ['name' => '', 'id' => '', 'disabled' => 'disabled']) 
    

    【讨论】:

      【解决方案3】:

      这可能不是您要查找的内容,但它会阻止用户选择第一个选项但仍然在列表中。

      Form::select支持A Grouped List,你可以这样使用。

      {!! Form::select('pet_type', ['Select Type' => ['dog', 'cat']], 0, ['class' => 'form-control']) !!}
      

      更多详情:http://laravel.com/docs/4.2/html#drop-down-lists

      【讨论】:

        【解决方案4】:

        查看源代码,这似乎是不可能的。 &lt;select&gt; 元素是在 https://github.com/illuminate/html/blob/master/FormBuilder.php#L532 建立的

        传递的唯一参数是值、名称和选定的布尔值。看起来你有 2 个解决方案。使用 javascript (argh),或使用 str_replace 之类的东西。

        <?php
        
            $field = Form::select('pet_type', ['Select Type','dog', 'cat'], 0, ['class' => 'form-control']);
        
            // find value="Select Type" and replace with value="Select Type" dialled
            echo str_replace('value="Select Type"', 'value="Select Type" disabled', $field);
        
        ?>
        

        【讨论】:

          【解决方案5】:

          只需在options 中传递disabled。试试 -

          {!! Form::select('pet_type', ['Select Type','dog', 'cat'], 0, ['class' => 'form-control', 'disabled' => true]) !!}
          

          您可以在 php 或使用 jquery 中手动循环遍历数组。

          $('select.someclass option:first').attr('disabled', true);
          

          【讨论】:

          • 就像我对 vanadium 说的:这是我首先做的,但它禁用了所有选择输入,我只想禁用第一个选项。
          【解决方案6】:

          最后一个数组用于构成html标签的属性,所以你可以简单地将disabled传入它:

              {!! Form::select('pet_type', ['Select Type','dog', 'cat'], 0, ['class' => 'form-control', 'disabled' => 'disabled']) !!}
          

          【讨论】:

          • 这是我首先做的,但它禁用了所有选择输入,我只想禁用第一个选项。
          猜你喜欢
          • 2014-09-10
          • 2018-04-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-10
          • 2014-11-20
          相关资源
          最近更新 更多