如何将自定义值从 PHP 发送到 sweetalert javascript 代码?

How to send custom value to sweetalert javascript code from PHP?

我想警告用户尝试删除文件。如何通过 html button 将一些详细信息传输到此 javascript ?例如,每次用户将删除不同的文件,因此该按钮应将不同的文件 ID 发送到 url.

fileid 部分

这是 .js 示例

    <script>
document.getElementById("custom-sa-warning") && document.getElementById("custom-sa-warning").addEventListener("click", function() {
    Swal.fire({
        html: '<div class="mt-3"><lord-icon src="https://cdn.lordicon.com/gsqxdxog.json" trigger="loop" colors="primary:#f7b84b,secondary:#f06548" style="width:100px;height:100px"></lord-icon><div class="mt-4 pt-2 fs-15 mx-5"><h4>Are you Sure ?</h4><p class="text-muted mx-4 mb-0">Are you Sure You want to Delete this Account ?</p></div></div>',
        showCancelButton: !0,
        confirmButtonClass: "btn btn-primary w-xs me-2 mb-1",
        confirmButtonText: "Yes, Delete It!",
        cancelButtonClass: "btn btn-danger w-xs mb-1",
        buttonsStyling: !1,
        showCloseButton: !0
    }).then((result) => {
   if (result.value) {
     window.location.href = `https://somedimain.com/delete.php?id=fileid`
   }
 });
})
</script>

这里是html启动弹窗

<button type="button" class="btn btn-primary" id="custom-sa-warning">Click me</button>

已经试过了 var test = 'https://somedimain.com/delete.php?id=$fileid';

if (result.value) {
       var example = test;
     window.location.href = example
   }

,它可以工作,但是如何通过按钮更改此 var test 值?

data-fileId 属性添加到您的按钮,当您点击按钮时,获得点击按钮 'data-fileId'。应该是这样的

html:

<button type="button" class="btn btn-primary" id="custom-sa-warning" data-fileId="write your fileId">Click me</button>

js:

<script>
document.getElementById("custom-sa-warning") && document.getElementById("custom-sa-warning").addEventListener("click", function(event) {
    var el = event.target || event.srcElement;
    var fileId = el.getAttribute('data-fileId');
    Swal.fire({
        html: '<div class="mt-3"><lord-icon src="https://cdn.lordicon.com/gsqxdxog.json" trigger="loop" colors="primary:#f7b84b,secondary:#f06548" style="width:100px;height:100px"></lord-icon><div class="mt-4 pt-2 fs-15 mx-5"><h4>Are you Sure ?</h4><p class="text-muted mx-4 mb-0">Are you Sure You want to Delete this Account ?</p></div></div>',
        showCancelButton: !0,
        confirmButtonClass: "btn btn-primary w-xs me-2 mb-1",
        confirmButtonText: "Yes, Delete It!",
        cancelButtonClass: "btn btn-danger w-xs mb-1",
        buttonsStyling: !1,
        showCloseButton: !0
    }).then((result) => {
    if (result.value) {
     window.location.href = "https://somedimain.com/delete.php?id=" + fileId
    }
  });
})
</script>