如何将值传递给 sweetalert?
How do I pass a value into a sweetalert?
我从数据库中填充了一系列项目。我想在每个上放置一条 sweetalert 更改状态确认消息。我如何将他们的 ID 传递到 sweetalert 确认消息中?
在该部分中,我有一个功能,如果他们确认消息,我想 运行 如果他们不确认,我不希望它做任何事情。我在页面底部有以下代码,单击 link 时会触发该代码。但是,我需要将 productID 传递到警报中,以便可以在 updateProducts 函数中调用它。
$(`.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.");
}
});
})
只需将您的 productID
存储在一个变量中,然后通过这样的函数传递它:
$(`.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.");
}
});
})
查看示例是您想要的吗?
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>
我从数据库中填充了一系列项目。我想在每个上放置一条 sweetalert 更改状态确认消息。我如何将他们的 ID 传递到 sweetalert 确认消息中?
在该部分中,我有一个功能,如果他们确认消息,我想 运行 如果他们不确认,我不希望它做任何事情。我在页面底部有以下代码,单击 link 时会触发该代码。但是,我需要将 productID 传递到警报中,以便可以在 updateProducts 函数中调用它。
$(`.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.");
}
});
})
只需将您的 productID
存储在一个变量中,然后通过这样的函数传递它:
$(`.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.");
}
});
})
查看示例是您想要的吗?
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>