简体   繁体   English

event.preventDefault()不起作用

[英]event.preventDefault() not working

i have this line of code in my view using the Telerik Grid: 我使用Telerik Grid在我的视图中有这行代码:

      columns.Bound(o => o.URI).Width(10).Sortable(false)
                .ClientTemplate("<A class='btnGrid' id=source<#= ID #> onclick=GridSelection.addItem('<#= ID #>') >Add</A>").Title("").Width(50);

the GridSelection addItem and disableSelected functions' JS codes: GridSelection addItem和disableSelected函数的JS代码:

  GridSelection = {
      addItem: function (value) {

         var anchorOption = $("a[id=source" + value + "]");

         anchorOption.click(function (e) { // variable name changed from "event"
               e.preventDefault();
               return false;    // as suggested by mr. Hamdi
               });

         anchorOption.fadeTo("slow", .5);

         GridSelection.disableSelected(anchorOption, true);

         var data = $("#GridSource").data('tGrid').data;
         var selectedObject;
         for (var item in data) {
            if (data[item].ID == value) {
               selectedObject = data[item];
               break;
            }
         }

          var grid = $("#GridSelected").data('tGrid');
          var newData = $("#GridSelected").data('tGrid').dataSource._data;
          newData.push(selectedObject);
          grid.dataBind(newData);
          grid.sort("");
          anchorOption.fadeTo("slow", .5);
      },

      disableSelected: function (element, disable) {
              //false on IEs 6, 7 and 8
              if (!$.support.leadingWhitespace) {
                  if (disable) {
                      $(element).attr('disabled', 'disabled');
                  } else {
                      $(element).removeAttr('disabled');
                  }
              }
     },
         // other GridSelection subfunctions here...

As I run the MVC3 web app in IE, it runs well because of the GridSelection.disableSelected function, but in Chrome and Mozilla Firefox , the event.preventDefault(); 当我在IE中运行MVC3 Web应用程序时,由于GridSelection.disableSelected函数,它运行良好,但在ChromeMozilla Firefox中event.preventDefault(); doesn't work. 不起作用。 The anchor link still adds the data item even after the user has already added it there. 即使用户已将数据项添加到那里,锚链接仍会添加数据项。

Is it OK having the preventDefault method inside the GridSelection.addItem function that was being prevented? 是否可以在GridSelection.addItem函数中使用preventDefault方法?

Which attribute is being prevented by the preventDefault , is it the href or is it the onclick ? preventDefault正在阻止哪个属性,它是href还是onclick

What wrong with this? 这有什么问题? How can I fix this bug? 我该如何解决这个错误? Anyone who can help? 有谁可以提供帮助?

The markup for your link has an inline click handler and no href : 链接的标记有一个内联点击处理程序,没有href

<A class='btnGrid' id=source<#= ID #> onclick=GridSelection.verify('<#= ID #>') >Add</A>

If it is the GridSelection.verify() that you're trying to prevent you should note that: 如果你试图阻止GridSelection.verify() ,你应该注意:

  1. The inline click handler is probably being called before your other event handler which means it is too late to cancel it from your other handler, and in any case 可能您的其他事件处理程序之前调用内联单击处理程序,这意味着从其他处理程序中取消它是为时已晚,并且无论如何
  2. e.preventDefault() doesn't stop other handlers from running, it stops the default action for the event which for a link is to navigate to the url specified in the href attribute. e.preventDefault()不会阻止其他处理程序运行,它会停止事件的默认操作,链接将导航到href属性中指定的url。 And you don't specify an href . 而且你没有指定一个href

If you want to have multiple handlers for the same event on the same element you should assign them all with jQuery in the order you want them to be called , and then use the event.stopImmediatePropagation() method within one of the handlers if you want to stop the rest of them from running. 如果你想在同一个元素上为同一个事件设置多个处理程序,你应该按照你希望它们被调用的顺序用jQuery分配它们,然后如果你想在其中一个处理程序中使用event.stopImmediatePropagation()方法阻止其他人逃跑

I don't know how to fit that within the code you've shown given that you don't show part of the code you want to prevent, but the general principle is: 我不知道如果你没有展示你想要防止的部分代码,你在所展示的代码中如何适应它,但一般原则是:

<a id="myLink" href="someURL">My link</a>

<script>
$("#myLink").click(function(e) {
    // some processing
    if (someCondition)
       e.stopImmediatePropagation();
    // some other processing
});

$("#myLink").click(function(e) {
    // some processing
});
</script>

Having said all that, if the .verify() function calls your .addItem() function and it is your intention to prevent future click events from working on the same link because the first click should verify/add and then you don't want to be able to add again, then within your .addItem() function do something like this: 说了这么多,如果.verify()函数调用你的.addItem()函数,并且你打算防止将来的点击事件在同一个链接上工作,因为第一次点击应该验证/添加然后你不想要为了能够再次添加,然后在.addItem()函数中执行以下操作:

anchorOption.removeAttr("onclick");
// or
anchorOption[0].onclick = null;

Have you tried adding return false; 你有没有尝试添加return false; instead of event.preventDefault(); 而不是event.preventDefault();

If the anchor has an id use the id selector instead of using it as attribute selector. 如果锚具有id使用id选择器而不是将其用作属性选择器。

Change 更改

$("a[id=source" + value + "]")

To

$("#source" + value)

I am not saying this is the issue but you can try changing this. 我不是说这是问题,但你可以尝试改变它。

Update: 更新:

event.preventDefault() stops the browser to follow the link set into href attribute of anchor element. event.preventDefault()使浏览器停止跟随设置为anchor元素的href属性的链接。 It will not prevent or stop the click event. 它不会阻止或停止click事件。

Have you tried renaming the event variable? 您是否尝试重命名事件变量? call me crazy but I seem to recall having issues just try function(e) {e.preventDefault();} and see what happens 叫我疯了,但我似乎记得有问题只是尝试function(e) {e.preventDefault();}看看会发生什么

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

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