【问题标题】:Calling a function when checking a checkbox, onclick event doesn't fire when unchecking选中复选框时调用函数,取消选中时不会触发onclick事件
【发布时间】:2013-05-13 22:47:17
【问题描述】:

我应该首先提到我正在使用 Internet Explorer 6。我正在从 onChange 事件调用 JavaScript 函数 (tabModifiedHighlight)。该功能在其他地方工作得很好,但是,当我选中复选框时,我在页面上有几个地方可以工作,但是当我取消选中它时,事件似乎甚至没有触发。 这是 JavaScript 函数:

function tabModifiedHighlight(){
    alert("alert");
    var div, i, input, inputIndex, selects, selectIndex, selectedTab, highlighted;
    var tabs = new Array("admissioninformation","diet","vitalsigns","activities","nursing","ivfluids","medications1","medications2","labs","respiratory","diagnostic","consultations");
    for(i=0; i<(tabs.length); i++){
        selectedTab = tabs[i]+'tab';
        if (document.getElementById(selectedTab).className == "selectedtab"){
            div = document.getElementById(tabs[i]),
            input = div.getElementsByTagName('input'),
            selects = div.getElementsByTagName('select');
            break;
        }
     }
    highlighted = false;
    for (inputIndex = 0; inputIndex < input.length; inputIndex++){
        if (input[inputIndex].checked == true){
        highlighted = true;
    }               
}
for (inputIndex = 0; inputIndex < input.length; inputIndex++){
    if (input[inputIndex].type == 'text' && input[inputIndex].value != ""){
        highlighted = true;
    }               
}
for (selectIndex = 0; selectIndex < selects.length; selectIndex++){
    if (selects[selectIndex].value != ""){
        highlighted = true;
    }               
}
if (highlighted == true){
    document.getElementById(selectedTab).style.backgroundColor = "#FF0";
}
else {
    document.getElementById(selectedTab).style.backgroundColor = "#F0F0F0";
}

}

这是调用它的输入:

<input name="cbMedTylenolPO" id="cbMedTylenolPO" type="checkbox" value="PO" onClick="tylenolPoShowHide(); checkBoxHighlight(this, 'MedicationsRow2'); tabModifiedHighlight();" />

此页面有多个“选项卡”,它们只是 div,根据选择的一个设置为可见或隐藏。它似乎是一致的,因为它在除 2 个选项卡之外的任何地方都可以工作,而在这些选项卡上无处可用。我能看到的唯一其他区别是,那些不工作的也显示或隐藏选项卡中的 div,这取决于复选框是否被选中。我在函数的最开始添加了警报,以查看它是否正在触发,并且在选中复选框时会触发,但在取消选中时不会。

我希望我说清楚了,任何想法都值得赞赏!

【问题讨论】:

    标签: javascript html internet-explorer-6 onchange


    【解决方案1】:

    由于您的代码不仅适用于两个标签,而且适用于所有其他标签,这不是浏览器兼容性问题。
    onClick 如果复选框您正在调用这 3 个方法 tylenolPoShowHide(); checkBoxHighlight(this, 'MedicationsRow2');tabModifiedHighlight()

    注意tabModifiedHighlight是最后一个..

    如果前两个方法tylenolPoShowHidecheckBoxHighlight 中的任何一个失败......那么tabModifiedHighlight 将不会被调用。

    我会建议在tylenolPoShowHidecheckBoxHighlight 中添加警报作为第一行和最后一行...

    它将帮助您找到实际失败的那个,然后您可以在此处添加该代码,我们将能够进一步帮助您

    【讨论】:

    • 非常感谢!通过按照您的建议在其他函数的开头和结尾添加警报进行调试,我能够确定它触发的时间实际上是在另一个函数的开头和结尾之间。经过仔细检查,我意识到如果选中了复选框,另一个函数正在调用 tabModifiedHighlight 函数。我仍然不知道为什么它没有调用我在 onClick 事件中拥有它的函数,但是通过修改另一个函数来调用它是否选中,我能够解决它,以便它每次都能正常工作现在。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 2013-02-02
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多