表元素的禁用属性的奇怪行为

Strange behavior of disabled property for table element

提问人:humbleCodes 提问时间:11/5/2023 最后编辑:humbleCodes 更新时间:11/5/2023 访问量:16

问:

因此,这是网站上提供的 .aspx 页面之一中的小型包装 HTML 元素。

<table id="ucDemographics_tdDemographic" style="width: 100%; height: 100%" cellspacing="0" cellpadding="3">
  <tbody> 
    <!--   inner html code removed for readability purspose  -->
  </tbody>
</table>

其中一个 js 函数正在尝试通过 id 访问此元素并执行一些逻辑:

function Validate() {
  
    // dropdown for physican name
    var physicVal = document.getElementById('ucDemographics_uxddlPhysicianName').value; 
  
    // Empty physicianName validation
    if (physicVal == "-1") {
        window.alert('Validation(s) failed');
        physicVal.focus();
        return false;
    }

    // Empty nurseName validation
    var tablebyID = document.getElementById('ucDemographics_tdDemographic');
    if (tablebyID.disabled == false) {
      
        // dropdown for nurse name
        var nurseVal = $("#ucDemographics_uxGENurse").val() ;
      
        if ( nurseVal == "-1") {
            window.alert('Validation(s) failed');
            nurseVal.focus();
            return false;
        }
    }
  
  return true;
}

我的问题是上面的 Validate 函数中的代码段如何能够访问 disabled 属性,因为它正在选择一个表标签,并且据我所知,它们不支持 disabled 属性。

var tablebyID = document.getElementById('ucDemographics_tdDemographic');
if (tablebyID.disabled == false) {  }

这是我的 chrome 调试器的快照:

enter image description here



编辑01:当 onClick() 事件在提交按钮上触发时,将调用 Validate 函数。

javascript HTML asp.net asp.net 网页

评论

2赞 Pointy 11/5/2023
浏览器完全可以处理 JavaScript 代码向 DOM 节点添加随机属性。为什么你的代码要这样做?好吧,我不知道,我也不知道其他人怎么会知道。
0赞 humbleCodes 11/5/2023
@Pointy但是,如果两个下拉列表中的任何一个将 value 属性设置为 -1,则问题在于编码逻辑,此时 tableID 变量可让您访问 disabled 属性。但是,如果我按顺序执行以下步骤,即 -> -> 正是在这些一系列操作中,我们无法访问禁用的属性。Both dropdown elements values are set to -1. and click save buttonThen alert message pops up as no physician name is not selectedSo, we select a physician from the dropdown and again click on Save button
1赞 humbleCodes 11/5/2023
@Pointy我想我明白了你的意思,谢谢你的帮助。必须有人手动设置为自定义属性disabled
0赞 Pointy 11/5/2023
是的,对我来说,这是一件有点奇怪的事情,但并不是很离谱或任何东西。就我个人而言,我会通过在元素中添加/删除一个“标记”类来做到这一点,该类与样式无关。

答: 暂无答案