简体   繁体   English

如何使用 Illuminate/Html 在 Laravel 中设置禁用选择选项

[英]How to set disabled select option in Laravel with Illuminate/Html

I'm starting with Laravel and I'm using Illuminate/Html for making forms.我从 Laravel 开始,我使用 Illuminate/Html 来制作表单。

I want to add disabled attribute to the first option and I dont find the way to do it.我想将禁用属性添加到第一个选项,但我没有找到方法。

{!! 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() !!}

Just pass the disabled in the options .只需在options传递disabled Try with -尝试 -

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

You can do it manually looping through the array in php or by using jquery.您可以手动循环遍历 php 中的数组或使用 jquery。

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

Looking through source, it looks like it's not possible.查看源代码,看起来这是不可能的。 The <select> element is built up at https://github.com/illuminate/html/blob/master/FormBuilder.php#L532 <select>元素建立在https://github.com/illuminate/html/blob/master/FormBuilder.php#L532

The only args passed is the value, the name and the selected boolean.唯一传递的参数是值、名称和选定的布尔值。 It looks like you have 2 solutions.看起来您有 2 个解决方案。 Use javascript (argh), or use something like str_replace .使用 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);

?>

as the signature of the function here : vendor/laravelcollective/html/src/FormBuilder.php line #625 :作为此处函数的签名:vendor/laravelcollective/html/src/FormBuilder.php line #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 = []
    )

so you can use it like this:所以你可以这样使用它:

{!! 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)
) !!}

This might not be exactly what you are looking for but it will prevent user to select the first option but still be in the list.这可能不是您正在寻找的确切内容,但它会阻止用户选择第一个选项但仍在列表中。

Form::select supports A Grouped List and you can use it this way. Form::select支持A Grouped List ,您可以通过这种方式使用它。

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

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

最后一个数组用于形成 html 标签的属性,因此您可以简单地将 disabled 传递给它:

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

这可以帮助你

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

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

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