更改单选按钮调用Java脚本功能

我的应用程序中有以下代码段,其中我创建了一个单选按钮以更改帖子的状态。

以下执行函数的方法给出了消息。

  

未捕获的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
                });
            }
        });
        }
    };
使用jquery方法:

@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。

我如何进行这项锻炼?

zhng41 回答:更改单选按钮调用Java脚本功能

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3161625.html

大家都在问