【问题标题】:Programmatically setting checkbox checked attribute in IE 10在 IE 10 中以编程方式设置复选框选中属性
【发布时间】:2017-12-08 22:29:59
【问题描述】:

我有一个遗留页面,它使用大量原始 javascript(没有隐藏浏览器差异的 JS 库)。它动态地创建一个这样的复选框:

function createCheckbox(checkboxName, isChecked, onClickHandler)
{
    var checkboxId = checkboxName + 'ChkBx';
    var chkbx;

    if(Ext.isIE)
    {
        // Following code does not work in IE10 when NOT using compatibility view
        // Throws JS error.  Worked in IE9.
        //if (isChecked)
        //{
        //    chkbx = document.createElement('<input id="' + checkboxId + '" type="checkbox" name="' + checkboxName + '" value="true" checked="checked" className="checkbox" />');
        //}
        //else
        //{
        //    chkbx = document.createElement('<input id="' + checkboxId + '" type="checkbox" name="' + checkboxName + '" value="true" className="checkbox" />');
        //}

        // Following code does not work in IE10 when using compatibility view
        // isChecked == true does not product checked checkbox
        chkbx = document.createElement('input');
        chkbx.id = checkboxId;
        chkbx.name = checkboxName;
        chkbx.value = 'true';
        chkbx.type = 'checkbox';
        chkbx.checked = isChecked;
        chkbx.className = 'checkbox';
    }
    else 
    { ... }

    return chkbx;    
}

我了解在 IE10 中,createElement() 不再接受复杂的字符串参数。这就是为什么我更改了上面的代码。我不明白为什么,它不尊重我将选中的属性设置为 true?应该如何指示复选框应该被选中?

【问题讨论】:

  • 应该可以正常工作; “isChecked”的值究竟是什么?

标签: javascript checkbox internet-explorer-10 checked


【解决方案1】:

添加

chkbx.defaultChecked = isChecked;

问题解决了。

当使用兼容性视图时,IE10 可能会退回到 IE7 的处事方式。 (取决于您的设置)

来源:Checkboxes will not check in IE7 using Javascript, and yet no errors

使用 IE 10.0.9200.16519 测试

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 2018-01-23
    • 2017-09-21
    • 1970-01-01
    • 2012-06-06
    • 2011-08-29
    • 2016-11-05
    相关资源
    最近更新 更多