gpt4 book ai didi

javascript - Semantic-ui 在模式对话框中忽略 e.preventDefault

转载 作者:行者123 更新时间:2023-12-03 06:46:09 25 4
gpt4 key购买 nike

我正在尝试使用语义 UI 来阻止从具有模式对话框的页面进行传输,但是,e.preventDefault() 似乎不起作用:

<!DOCTYPE html>
<html>
<head>
<title>Modal Block HREF</title>

<link rel="stylesheet" type="text/css" href="node_modules/semantic-ui/dist/semantic.min.css">
<script src="node_modules/jquery/dist/jquery.min.js"></script>
<script src="node_modules/semantic-ui/dist/semantic.min.js"></script>
</head>
<body>

<a class="dirty" href="http://www.microsoft.com">Microsoft</a>
<a class="dirty" href="http://www.google.com">Google</a>

<div id="confirmmodal" class="ui small modal">
<div class="header">FIRST</div>
<div class="content">
<div class="left">
The first of many interesting things
</div>
</div>
<div class="actions">
<div class="ui black deny button">
Cancel
</div>
<div class="ui positive button">
OK
</div>
</div>
</div>

<div id="savemodal" class="ui small modal">
<div class="header">SECOND</div>
<div class="content">
<div class="left">
The second of many interesting things
</div>
</div>
<div class="actions">
<div class="ui black deny button">
Cancel
</div>
<div class="ui positive button">
OK
</div>
</div>
</div>

<script type="text/javascript">
$('.dirty').on('click', function(e, t) {
// This works...
//if (confirm('Are you sure')) {
// console.log("allow transfer");
//}
//else {
// console.log("block transfer");
// e.preventDefault();
//}

// This does NOT work
$('#confirmmodal')
.modal({
onApprove: function () {
console.log("allow transfer");
},
onDeny: function () {
console.log("prevent transfer");
e.preventDefault();
}
})
.modal('show');
});
</script>
</body>
</html>

似乎发生的情况是在对话框出现之前立即响应链接,即使我快速单击“取消”,也没有什么区别。

最佳答案

您需要阻止第一次点击提交页面。使用下面的内容

<script type="text/javascript">
$('.dirty').on('click', function(e, t) {
e.preventDefault();
var href = $(this).attr("href");
$('#confirmmodal') .modal({
onApprove: function () {
console.log("allow transfer");
window.location = href;
},
onDeny: function () {
console.log("prevent transfer");
}
}) .modal('show');
});
</script>

关于javascript - Semantic-ui 在模式对话框中忽略 e.preventDefault,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37726643/

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