复选框onchange功能在IE和Chrome中无法正常工作

checkbox onchange function does not work as expected in IE and Chrome

本文关键字:常工作 工作 功能 onchange IE Chrome 复选框      更新时间:2023-09-26

我在页面上动态生成了复选框。我使用onchange函数,它在Firefox中工作得很好:

function setActionDate(cb_id,dt_id){
    //alert(cb_id);
    var cbox = document.getElementById(cb_id);
    var ddate= document.getElementById(dt_id);
    alert(cbox.value);
    if (cbox.value == 'Y') {
            ddate.className = "";
            ddate.style.backgroundColor="#ffe6e6";  
            ddate.placeholder = "Select Date";
    } else if (cbox.value == 'N'){
            ddate.value = "";
            ddate.className = "disabled";
            ddate.style.backgroundColor="";
            ddate.placeholder = "";
    }
}

我在这里读到IE对待onchange事件不同,最好使用onclick函数。然而,我绝对需要使用 oncahnge 函数。有办法解决它的IE和Chrome?

我找到了解决问题的正确方法。使用setTimeout函数解决了这个问题:

function setActionDate(cb_id,dt_id){
    setTimeout(function() { 
    var cbox = document.getElementById(cb_id);
    var ddate= document.getElementById(dt_id);
    alert(cbox.value);
    if (cbox.value == 'Y') {
            ddate.className = "";
            ddate.style.backgroundColor="#ffe6e6";  
            ddate.placeholder = "Select Date";
    } else if (cbox.value == 'N'){
            ddate.value = "";
            ddate.className = "disabled";
            ddate.style.backgroundColor="";
            ddate.placeholder = "";
    }
  },100);
}