如何在Blade语法中使用JavaScript变量?我收到了错误:
使用未定义的常量buttonID
var t = $("#datatable").DataTable({
"order": [[ 1, 'asc' ]],
"ajax": "questions1/get-data",
"deferRender": true,
"processing": true,
sAjaxDataProp: "",
"columns": [
{ "data": "id" },
{ "data": "name" },
{ "data": "description" },
{ "data": "answers.[, ].name" },
{ "data": "campaigns.[, ].name" },
{ "data": "label" },
{
sortable: false,
"render": function ( data, type, full, meta ) {
var buttonID = full.id;
return '@can('delete', $question){!! Form::open(['method'=>'DELETE','url' => ['/admin/survey-details/questions', buttonID ],'style' => 'display:inline']) !!}{!! Form::button('<span class="glyphicon glyphicon-trash" aria-hidden="true" title="Delete Question" />', array('type' => 'submit','class' => 'btn btn-danger btn-xs','title' => 'Delete Question','onclick'=>'return confirm("Confirm delete?")')) !!}{!! Form::close() !!}@endcan';
}
}
],
});
答案 0 :(得分:0)
您无法从客户端执行php,您可以使用串联将id附加到使用js的URL的末尾
var t = $("#datatable").DataTable({
"order": [[ 1, 'asc' ]],
"ajax": "questions1/get-data",
"deferRender": true,
"processing": true,
sAjaxDataProp: "",
"columns": [
{ "data": "id" },
{ "data": "name" },
{ "data": "description" },
{ "data": "answers.[, ].name" },
{ "data": "campaigns.[, ].name" },
{ "data": "label" },
{
sortable: false,
"render": function ( data, type, full, meta ) {
var buttonID = full.id;
return '@can('delete', $question){
<form action="{{url("/admin/survey-details/questions")/'+buttonID+'"style="display:inline">
<input type="hidden" name="_method" value="delete">
{{ csrf_field() }}
<button type="submit" class="btn btn-danger btn-xs" title="Delete Question" onclick=>"return confirm('Confirm delete?')"><span class="glyphicon glyphicon-trash" aria-hidden="true" title="Delete Question" /></button>
</form>
}@endcan';
}
}
],
});