gpt4 book ai didi

javascript - 在切换按钮中从开切换到关(显示警报)以及从关切换到开(不显示警报)

转载 作者:行者123 更新时间:2023-12-01 01:39:33 24 4
gpt4 key购买 nike

我有切换开关按钮,我想在它从开到关时显示确认警报(“您确定吗,您想关闭状态!”),而当它从关到开时,它不应该显示显示确认警报。

我做了如下操作,但是当它从“关闭”变为“打开”时,它将再次显示警报:)谁能帮助我或为我指明正确的方向!
预先感谢:)

JavaScript:

$(document).ready(function() {

$('.switchOnOff').click(function() {

var Id = $(this).data('id');
var checkStatus = $(this).find('input:checkbox').is(':checked') ? 'On' : 'Off';

if (checkStatus == "Off") {
if (confirm("Are you sure , you want to trun off status!")) {
$.ajax({
type: "POST",
url: '@Url.Action("UpdateNtf", "Account")',
data: {
id: Id,
status: checkStatus
},
dataType: 'json',
success: function(result) {

if (result) {

console.log("Ok");
} else {

console.log("error");
}

},
error: function() {
console.log('something went wrong - debug it!');
}
})

} else {
return false;
}
}
});
});

查看:

      @if (rma.NTFonoff == "On")
{
<input type="checkbox" class="switch-input" checked>
<span data-on="On" data-off="Off" class="switch-label"></span>
}
else

{
<input type="checkbox" class="switch-input">
<span data-off="Off" data-on="On" class="switch-label"></span>
}

最佳答案

这是一个示例代码,可以满足您的要求。我不会编辑您的代码,因为其中有很多与您的问题无关的额外困惑。

const button = document.getElementById('button')

button.addEventListener('click', function () {
if (this.innerText == 'On') {
if (confirm('Are you sure?')) {
this.innerText = 'Off'
}
} else {
this.innerText = 'On'
}
})
<button id="button">Off</button>

关于javascript - 在切换按钮中从开切换到关(显示警报)以及从关切换到开(不显示警报),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52544993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com