如何在KnockoutJs点击事件处理程序中使用event.preventDefault? [英] How do I use event.preventDefault with KnockoutJs click event handlers?

查看:113
本文介绍了如何在KnockoutJs点击事件处理程序中使用event.preventDefault?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下表格行作为KnockoutJs中的脚本模板:

I have the following table row as a script template in KnockoutJs:

    <script id="inboxTemplate" type="text/html">           
    <tr data-bind="click: function() { viewInboxModel.selectAction($data); }">                                 
        <td>...</td>                 
        <td>${ CreateDate }</td>               
        <td data-bind="click: function(e){ e.preventDefault();viewInboxModel.clearAction($data); }"><img src="/Content/images/delete.png" height="16px" width="16px"> </td>
    </tr>            
    </script>

问题是当我单击删除按钮时,它也在运行selectAction()方法.我试过在selectAction单击处理程序和clearAction()单击处理程序中都使用e.preventDefault()无济于事.无论如何,如果单击删除按钮,KnockoutJs可以阻止现在单击基础行吗?

The problem is when I click the delete button, it is also running the selectAction() method. I've tried using e.preventDefault() in both selectAction click handler and the clearAction() clicker handler to no avail. Is there anyway KnockoutJs can prevent the underlying row from now being clicked if the delete button is clicked?

推荐答案

如果您引用了jQuery,则可以在处理程序中安全地调用e.stopImmediatePropagation();,因为它已传递给jQuery事件对象.如果您不使用jQuery,那么您仍然可以做类似的事情:

If you have jQuery referenced, then you can safely call e.stopImmediatePropagation(); in your handler, as it is passed the jQuery event object. If you are not using jQuery, then you could still do something like:

e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();

http://jsfiddle.net/rniemeyer/mCxjz/

这篇关于如何在KnockoutJs点击事件处理程序中使用event.preventDefault?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆