单击问题 - 确认警报不会阻止 HREF 单击
我正在尝试完成以下任务:
当用户单击包含复选框的 div("productListMatch") 外部时,如果选中任何复选框,则会出现确认警报,让用户知道他们尚未保存在那里复选框选择。
简而言之,我正在检查选中的项目是否已保存。如果是这样,请通知用户,让他们选择保存或不保存。
这就是我所做的:
function formCheckboxConfirm() {
$('html').click(function() {
var elem = document.getElementById('frm-list-product').elements;
for (i = 0; i < elem.length; i++) {
if(elem[i].type == "checkbox" && elem[i].checked) {
confirm("You have made changes! Do you want to proceed without saving your changes?");
break;
}
}
});
$(".productListMatch").click(function(e) {
e.stopPropagation(); // I thought this would stop all link clicks, but it's not working as I expected.
});
}
虽然这通常有效,但它不适用于链接。单击它们后,它们会继续执行,并且我仍然会收到确认警报。
注意:网页上的 div 之外有许多导航链接。 “productListMatch”div 本身是 jquery 选项卡(两个中的第二个)内内容的一部分,因此必须单击该选项卡才能看到“productListMatch”div。
我想要的是这样的:
- 如果单击除复选框(和“保存”按钮)之外的任何链接
- 停止链接执行
- 检查复选框是否被选中
- 如果选中复选框,则提供确认警报
- 如果用户点击“确定”,网页将继续访问最初点击的链接
- 如果用户点击“取消”,网页将不会继续访问最初点击的链接
- 如果未选中某个复选框,则不会出现确认警报,并且网页会继续链接最初选中的链接
- 如果选中复选框,则提供确认警报
- 检查复选框是否被选中
- 停止链接执行
,我尝试了几种方案,虽然我可以让链接停止(我使用了 PreventDefault),但我不确定如何让它们重新启动。也许我对这整件事的看法都是错误的。
我希望得到一些帮助。
谢谢。
斯蒂芬
I'm trying to accomplish the following:
When a user clicks outside of a div("productListMatch") containing checkboxes, if any of the checkboxes are checked, a confirm alert appears, letting the user know that they haven't saved there checkbox selection.
In short, I'm checking if a checked item is saved or not. If so, notify the user, giving them the option to save or not.
This is what I've done:
function formCheckboxConfirm() {
$('html').click(function() {
var elem = document.getElementById('frm-list-product').elements;
for (i = 0; i < elem.length; i++) {
if(elem[i].type == "checkbox" && elem[i].checked) {
confirm("You have made changes! Do you want to proceed without saving your changes?");
break;
}
}
});
$(".productListMatch").click(function(e) {
e.stopPropagation(); // I thought this would stop all link clicks, but it's not working as I expected.
});
}
Although this works in general, it doesn't work with links. Once they're clicked, they continue to perform, and I still receive the confirm alert.
Note: There are a number of navigation links on the webpage outside of the div. The "productListMatch" div itself is a part of content within a jquery tab (2nd of two), so the tab has to be clicked just to see the "productListMatch" div.
What I'd like is this:
- If any link - aside from the checkboxes (and the Save button) - is clicked
- Stop link from performing
- Perform check if checkboxes are checked or not
- If a checkbox is checked, provide confirm alert
- If user clicks "OK", webpage continues to link that was initially clicked
- If user clicks "CANCEL", webpage does not continues to link that was initially clicked
- If a checkbox is not checked, no confirm alert, and webpage continues to link that was initially checked
- If a checkbox is checked, provide confirm alert
- Perform check if checkboxes are checked or not
- Stop link from performing
I tried couple of scenarios, and although I can get the links to stop (I used preventDefault), I'm uncertain as how to get them to start again. Perhaps I'm going about this whole thing wrong.
I would appreciate some assistance.
Thanks.
Stephen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
confirm()
函数返回一个您需要测试的布尔值。例如:The
confirm()
function returns a boolean you need to test. For instance: