我的应用程序中有以下代码段,其中我创建了一个单选按钮以更改帖子的状态。
以下执行函数的方法给出了消息。
未捕获的ReferenceError:未定义发布 在HTMLInputElement.onchange
ProctrackType=proctrack/linuxproc
function publish() {
let publish = $(this).data('publish');
if ($(this).prop("checked")) {
$.ajax({
type: "POST",url: '/manage/post/publish/' + publish,data: {
'_token': $('meta[name="csrf-token"]').attr('content'),'id': publish,"_method": 'PUT'
},success: function (data) {
Toast.fire({
type: data.type,title: data.message
});
}
});
}
else {
$.ajax({
type: "POST",url: '/manage/post/unpublish/' + publish,title: data.message
});
}
});
}
};
@foreach($collection as $item)
<div class="form-group mt-2 mb-2">
<span class="switch switch-sm">
<input type="checkbox" class="switch" id="status" data-publish="{{$item->id}}" {{$item->status ? 'checked' : ''}} onchange="publish()">
<label for="status" class="mb-0">Publish</label>
</span>
</div>
@endforeach
它仅在循环的第一次迭代中起作用。由于$("#status").click(function () {
..............
});
也在循环中被迭代。 ID
捕获第一个ID。
我如何进行这项锻炼?