简体   繁体   English

如果调用了preventDefault(),则无法以编程方式选择单选按钮

[英]Cannot programmatically select radio button if preventDefault() called

I've got a 'catch 22' in Chrome. 我在Chrome中有一个“ catch 22”。 I cannot programmatically select a radio button within a click event if any other function bound to the same event makes a call to preventDefault() . 如果绑定到同一事件的任何其他函数调用了preventDefault()我无法以编程方式选择click事件中的单选按钮。

For example, I have a radio button with a parent element bound to a click event in which preventDefault() is called. 例如,我有一个单选按钮,其父元素绑定到click事件中,在该事件中preventDefault() If the radio button is clicked directly it is not checked. 如果直接单击单选按钮,则不会选中它。 This is to be expected. 这是预料之中的。 However, I actually need the radio button to be selected so within code I attempt to check it in another function bound to the click event: $(this).prop('checked', true); 但是,实际上,我需要选择单选按钮,因此在代码中,我尝试在绑定到click事件的另一个函数中检查它: $(this).prop('checked', true); .

Oddly, this doesn't work and I cannot remove the call to preventDefault() or disable propagation because it is in third party code that I need to run. 奇怪的是,这行不通,并且我无法删除对preventDefault()的调用或禁用传播,因为它在我需要运行的第三方代码中。

Is this a bug? 这是错误吗? Any suggested workarounds? 有建议的解决方法吗?

Here is an example: http://jsfiddle.net/LnLuk4st/ 这是一个示例: http : //jsfiddle.net/LnLuk4st/

UPDATE: 更新:

I have tried @RGraham's suggestion. 我已经尝试过@RGraham的建议。 His example clearly works, but oddly it does not work in the context of my code . 他的示例显然有效,但奇怪的是,在我的代码上下文中它不起作用 @RGraham's code had a syntax error which made it appear to be working. @RGraham的代码存在语法错误,这使其似乎正常工作。

Here's some context: 这里是一些上下文:

// Remember kendo tab
$(".k-tabstrip").each(function () {
    var $tabStrip = $(this);
    var $tabs = $tabStrip.find(".k-tabstrip-items .k-item");
    var tabCookie = "Current_Tab_" + $tabStrip.attr("id");

    // On tab change, set cookie
    $tabs.click(function () {
        createCookie(tabCookie, $(this).attr("aria-controls"), 1);
        $tabStrip.parent().css({ 'min-height': $tabStrip.parent().height() });


        if ($(this).is('input')) { // Doesn't eval to true, 'this' is always a '.k-item'.
            $(this).prop("checked", true);
        } else {
            // Never works if the input is clicked directly
            $(this).find('input').prop("checked", true);
        }


    });

    // @RGraham's suggestion...
    $tabs.on('click', 'input', function() {
        $(this).prop("checked", true); // Line reached but doesn't work either :(
    });

    // If cookie set, select tab
    var tab = readCookie(tabCookie);

    if (tab) {
        $tabs.each(function () {
            if ($(this).attr("aria-controls") == tab) {
                $(this).click();
            }
        });
    }
});

I still believe this behaviour to be a bug but I have found a workaround. 我仍然认为此行为是一个错误,但是我找到了一种解决方法。

Capture the click of the radio button directly, prevent propagation, then programmatically click the parent of the radio button. 直接捕获单选按钮的单击,防止传播,然后以编程方式单击单选按钮的父级。 This allows the third party code to run without applying preventDefault to the radio button. 这允许运行第三方代码,而无需将单选按钮应用preventDefault

    // preventDefault bug fix.
    $tabs.find("input").click(function (e) {
        e.stopPropagation();
        $(this).parent().click();
    });

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

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