0

当用户单击复选框时,我正在禁用单选按钮组,仅使用原始 javascript 和 disabled 属性。

我的功能很简单:

function toggleEnabled(elementId) {
  e = document.getElementById(elementId)
  e.disabled = !e.disabled;
}

并使用 onClick 事件调用它,例如 onClick="toggleEnabled('radio_div')"

它工作得很好,但是如果用户单击返回,浏览器似乎会记住复选框的状态,但会将 div 中组件的状态重置为它们原来的状态。

这是在 IE7 中,我现在不想使用 JS 库,所以请不要提出任何建议。

难道我做错了什么?是否有获得预期行为的解决方案(记住复选框和返回上的 div 的状态)?

4

1 回答 1

1

您必须检查 onload 方法中的每个单选框值:

var selects = document.getElementByTagName("select");
for ( var i = 0 ; i < selects.length ; i ++ )
{
    //call your code if an option is checked
    if ( selects.options[selects.selectedIndex] )
    { 
        var selectedOption = selects.options[selects.selectedIndex];
        //now you got your option and can enable the div.
        toggleEnabled('radio_div');// <-- change this depending on selectedOption
    }
}
于 2009-10-14T14:29:55.863 回答