【问题标题】:From jQuery to blade variable从 jQuery 到刀片变量
【发布时间】:2017-08-07 10:33:23
【问题描述】:

我知道如何将变量从刀片模板传递到 jQuery 模态,但我不知道如何以其他方式进行。例如,我可以检查用户是否已注册:

@if(\Illuminate\Support\Facades\Auth::user())
    open modal with options
@else
    open basic modal message "you are not authorized"
@endif

但是现在,我想检查一下注册用户是否可以使用他点击的设备。我有所有必要的模型关系,所以我可以做类似Auth::user()->equipment->where('equipment_id', $equipment->id) != null...但问题是我可以像data-equipment-id = ... 这样将变量转发给jQuery并在脚本中获取它...但是我怎么得到它回到 PHP 变量,所以我可以像这样访问它:

@if(\Illuminate\Support\Facades\Auth::user())
    @if(Auth::user()->equipment->where('equipment_id', $equipment->id) != null)
        allowed!
    @else
        not allowed!
    @endif
@else
    open basic modal message "you are not authorized"
@endif

编辑:

这是我将数据转发到模态的方式:

@foreach($equipment as $instrument)
    <td>
        <a href="#" style="display: block;"
           data-href="{{route('reserve-me', [$instrument->id, $scheduler_start_time->timestamp])}}"
           data-toggle="modal"
           data-target="#confirm-reservation"
           data-start-time="{{$scheduler_start_time->toW3cString()}}"
           data-end-time="{{$scheduler_end_time->toW3cString()}}"
           >

            @if($instrument->reservations
            ->where('reserved_from','<=', $scheduler_start_time)
            ->where('reserved_to','>=', $scheduler_start_time)->first() != null)

                HERE

            @else
                &nbsp;
            @endif
        </a>
    </td>
@endforeach

然后是模态脚本:

$('#confirm-reservation').on('show.bs.modal', function (e) {
    $(this).find('.dropdown-menu li').remove();
    $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
    var startTime = moment($(e.relatedTarget).data('start-time')).utc();
    var endTime = moment($(e.relatedTarget).data('end-time')).utc();

    while (startTime < endTime) {
        $(this).find('.dropdown-menu').append('<li><a href="#">' + startTime.format("HH:mm") + '</a></li>');
        startTime.add(30, 'minutes');
    }

    $(document).on('click', '#confirm-reservation .dropdown-menu a', function () {
        $('#insert_text').text($(this).text());
        var href = $(e.relatedTarget).data('href');
        var time = moment().year(startTime.format("Y")).month(startTime.format("M")).date(startTime.format("D"))
                .hour(($(this).text().split(':')[0]) - 2).minutes($(this).text().split(':')[1]).seconds("0");

        console.log(time);

        $('.btn-ok').attr('href', href + '/' + time.unix());
    });
});

【问题讨论】:

标签: php jquery laravel blade


【解决方案1】:

使用 jquery 和 ajax 来实现:

$.ajax({
    url: 'your route',                        
    type: 'POST',                                  
    data: {
        var1 :val1,
    },
    success: function(response){
        // response from controller function which contains a message in it
        // pass it to modal or dialog like

        $('#dialog').html(response);
        $('#dialog').dialog();

    }
});

【讨论】:

  • 感谢您的回答...我已经用更多代码更新了我的问题,您能否更具体一些?我只能将变量推送到模态脚本
猜你喜欢
  • 2022-01-01
  • 2017-09-22
  • 2016-09-18
  • 2015-10-16
  • 2022-11-02
  • 2019-01-19
  • 2018-08-15
  • 2014-12-08
  • 2017-02-24
相关资源
最近更新 更多