简体   繁体   English

如何将值传递给 sweetalert?

[英]How do I pass a value into a sweetalert?

I have a series of items populated from a database.我有一系列从数据库中填充的项目。 I want to put a sweetalert change status confirmation message on each.我想在每个上都放一条甜蜜的更改状态确认消息。 How do I pass their ID into a sweetalert confirmation message?如何将他们的 ID 传递到 sweetalert 确认消息中?

In the section, I have a function which I want to run if they confirm the message, and if they don't I don't want it to do anything.在该部分中,我有一个 function 如果他们确认消息我想运行它,如果他们不希望它做任何事情。 I have the following code at the bottom of the page which is triggered when a link is clicked.我在页面底部有以下代码,单击链接时会触发该代码。 However, I need to pass the productID into the alert so that it can be called in the updateProducts function.但是,我需要将 productID 传递到警报中,以便可以在 updateProducts function 中调用它。

$(`.link`).click(function () {
    swal({
        title: "Are you sure?",
        text: "Are you sure you want to change the status?",
        icon: "warning",
        buttons: true,
        dangerMode: true,
    }).then((confirm) => {
        if (confirm) {
            updateProducts(productID);
        } else {
            swal("The status has not changed.");
        }
    });
})

Just store your productID in a variable and pass it through a function like this:只需将您的productID存储在一个变量中,然后像这样通过 function 传递它:

$(`.link`).click(function () {

    var productID = 'productID';
    
    swal({
        title: "Are you sure?",
        text: "Are you sure you want to change the status?",
        icon: "warning",
        buttons: true,
        dangerMode: true,
    }).then((confirm) => {
        if (confirm) {
            updateProducts(productID);
        } else {
            swal("The status has not changed.");
        }
    });
})

See example is it what you want?看例子是你想要的吗?

 function updateProducts(y) { alert(y); } function someFunc(x) { swal({ title: x, // <-- Passed Value text: "Are you sure you want to change the status?", icon: "warning", buttons: true, dangerMode: true, }).then((confirm) => { if (confirm) { updateProducts(x); // <-- do your code } else { swal("The status has not changed."); } }); } let str = "My Value"; $(`.link`).click(function () { someFunc(str); // <-- run this on click event. })
 .link { padding:30px; background: #40cc40; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js" integrity="sha512-AA1Bzp5Q0K1KanKKmvN/4d3IRKVlv9PYgwFPvm32nPO6QS8yH1HO7LbgB1pgiOxPtfeg5zEn2ba64MUcqJx6CA==" crossorigin="anonymous"></script> <button class="link">Click Me</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM