【问题标题】:Add disable attribute on select options using function return html使用函数返回 html 在选择选项上添加禁用属性
【发布时间】:2018-05-02 21:12:59
【问题描述】:

我正在使用此功能在选择框中添加选项。选项已成功添加,但我不知道使用函数添加额外属性。那么如何在值为 0 的第一个选项上添加额外的属性,例如禁用 <option value="0" disable>Select Fee Head</option>

功能

public function feeHtmlRow()
    {
        $fee_heads = [];
        $fee_heads[0] = 'Select Fee Head';
        foreach (FeeHead::select('id', 'fee_head_title')->get() as $fees) {
            $fee_heads[$fees->id] = $fees->fee_head_title;
        }
        $response['html'] = view($this->view_path.'.includes.fee_tr', [
            'fee_heads' => $fee_heads
        ])->render();
        return response()->json(json_encode($response));

    }

jQuery

$('#load-html-btn').click(function () {

            $.ajax({
                type: 'POST',
                url: '{{ route('account.fees.master.fee-html') }}',
                data: {
                    _token: '{{ csrf_token() }}',
                },
                success: function (response) {
                    var data = $.parseJSON(response);

                    if (data.error) {
                       // $.notify(data.message, "warning");
                    } else {

                        $('#fee_wrapper').append(data.html);
                        //$.notify(data.message, "success");

                    }
                }
            });

        });

输出

<select class="form-control" required="" name="fee_head[]">
     <option value="0">Select Fee Head</option>
     <option value="2">APPLICATION FORM</option>
     <option value="1">MONTHLY FEE</option>
     <option value="4">REGISTRATION FEE</option>
     <option value="3">TUTION FEE</option>
</select>

【问题讨论】:

  • 你没有展示&lt;select&gt;是如何生成的

标签: javascript php jquery mysql laravel-5


【解决方案1】:

使用jQuery,选择第一个选项并将其禁用,或者您可以在生成select 时将disable 添加到您的第一个选项中

$('option[value="0"]').attr("disabled", "disabled");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="form-control" required="" name="fee_head[]">
  <option value="0">Select Fee Head</option>
  <option value="2">APPLICATION FORM</option>
  <option value="1">MONTHLY FEE</option>
  <option value="4">REGISTRATION FEE</option>
  <option value="3">TUTION FEE</option>
</select>

更新

添加选项后,使用$(document).find('option[value="0"]').attr("disabled", "disabled"); 禁用第一个选项。

$('#load-html-btn').click(function () {

        $.ajax({
            type: 'POST',
            url: '{{ route('account.fees.master.fee - html') }}',
            data: {
                _token: '{{ csrf_token() }}',
            },
            success: function (response) {
                var data = $.parseJSON(response);

                if (data.error) {
                    // $.notify(data.message, "warning");
                } else {

                    $('#fee_wrapper').append(data.html);
                    //$.notify(data.message, "success");
                    // disable the first option after appending the select
                    $(document).find('option[value="0"]').attr("disabled", "disabled");
                }
            }
        });

    });

【讨论】:

  • 什么时候可以添加 $('option[value="0"]').attr("disabled", "disabled");在我的 jquery 上
  • @UmeshYadav 如果这已经解决了您的问题,请选择它作为答案。
猜你喜欢
  • 2021-05-10
  • 1970-01-01
  • 2018-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多