简体   繁体   English

jQuery的单击功能,除了之一?

[英]JQuery on click function, except one?

I'm using javascript to display a notification when a user clicks on an tag (Are you sure you want to leave this page?) but I don't want it to work on the one that posts the form. 我正在使用javascript在用户单击标签时显示通知(您确定要离开此页面吗?),但我不希望它在发布表单的标签上起作用。 Is there any way of making it work for all tags except one? 有什么方法可以使其对除一个标签之外的所有标签起作用?

$(document).ready(function()
{
    $('a').on('click', function()
    {
        var note=$("#formnote").val();
        if (note.length > 0){
            var redirectquestion = confirm("Are you sure that you want to leave this page?");
            if(!redirectquestion ){
                return false;
            }
        }
    });
});
$(document).ready(function () {
    $("a:not(#link_submit)").on('click', function () {
        var note = $("#formnote").val();
        if (note.length > 0) {
            var redirectquestion = confirm("Are you sure that you want to leave this page?");
            if (!redirectquestion) {
                return false;
            }
        }
    });
});

Something like this should work if I understand te question right (and assuming you add the class submitLink to the tag you don't want to trigger this call from 如果我理解正确的问题(并且假设您将类submitLink添加到标签中,则您不想触发此调用),这样的操作应该会起作用

$(document).ready(function()
{
  $('a:not(.submitLink)').on('click', function()
  {
    var note=$("#formnote").val();
    if (note.length > 0){
      var redirectquestion = confirm("Are you sure that you want to leave this page?");
      if(!redirectquestion ){
        return false;
      }
    }
  });
});

I would use jQuery's .not() so that you're not binding an event handler to that element and using resources to not do anything. 我将使用jQuery的.not(),这样您就不会将事件处理程序绑定到该元素,也不会使用资源执行任何操作。 Also, you can easily assign a click handler to that element later without worrying about your previous code. 此外,您以后可以轻松地将点击处理程序分配给该元素,而不必担心您之前的代码。 So something like this: 所以像这样:

$('a').not('.submitLink').on('click', function() {
    ...
});

With not selector : 使用非选择器:

$(document).ready(function () {
$('a:not(#link_submit)').on('click', function () {
    var note = $("#formnote").val();
        if (note.length > 0) {
            var redirectquestion = confirm("Are you sure that you want to leave this page?");
            if (!redirectquestion) {
                return false;
            }
        }
    });
});

With .not() : 使用.not():

$(document).ready(function () {
$('a').not('#link_submit').on('click', function () {
    var note = $("#formnote").val();
        if (note.length > 0) {
            var redirectquestion = confirm("Are you sure that you want to leave this page?");
            if (!redirectquestion) {
                return false;
            }
        }
    });
});

Have a nice day. 祝你今天愉快。

This should work also - if your a-tag has the id 'submit'. 如果您的a-tag的ID为“ submit”,这也应该起作用。

$(document).ready(function () {
    $('a').not('#submit').on('click', function () {
        var note = $("#formnote").val();
        if (note.length > 0) {
            var redirectquestion = confirm("Are you sure that you want to leave this page?");
            if (!redirectquestion) {
                return false;
            }
        }
    });
});

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

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