JavaScript:禁用按钮不会变灰 [英] JavaScript: Disabled button not grayed out

查看:108
本文介绍了JavaScript:禁用按钮不会变灰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的帖子中: JavaScript - 设置控件的禁用未成功 [ ^ ] ,我报告说动态设置控件的残疾是不成功的。在调试代码时,控制值是正确的。但是,控件不会变为灰色,因为其disabled = true。我觉得在我的HTML中的某个地方,有一些不正确的东西。如何检测此错误?谢谢。



  function  validate_Panel7(){
调试器;
var v = document .getElementById(' btnValidate7');
if (v.disabled == false
v.disabled = < span class =code-keyword> true
;
else
return ;
}

解决方案

在某些条件下,它应该可以工作。



它可能由于不同的原因而起作用。例如, document.getElementById 可以返回null,因为可能找不到该元素; btnValidate7应该是有用元素的属性 id 的值,而且,所有 id 值应该是唯一的整个页面的上下文。一个无效的 id 值会使整个页面不正确。在这种情况下,接下来会发生以下情况: v.disabled 尝试引用 v ,这是null,在转而抛出一个你根本看不到的异常,因为你没有处理异常。在浏览器中,未处理的JavaScript异常被阻止在顶部;它们没有引起注意。



在其他情况下,可能会发现找到的元素不是输入元素;使用这些元素,您不能使用禁用。另外,我看不出这个函数的调用位置;我不知道它是被调用还是被调用的执行阶段。如果元素尚未创建或未呈现怎么办?



为什么毕竟你把这个调试器声明?如果需要调试代码,请实际调试它。您将在每一步中看到所有对象的所有值。



这是一个完整的最小HTML,带有脚本供您比较;它确实有效:

 <   html  >  
< head >
< title > 禁用测试< / title >
< / head >
< 正文 >

< 按钮 id = btnValidate7 > 测试< / button >

< 脚本 >
function validatePanel(){
var v = document.getElementById('btnValidate7');
v.disabled = true;
}
validatePanel();
< / script >

< / body >
< / html >



这不是你遇到的最大问题。更大的问题是你如何编程(并提出问题)。例如,查看 if 。可能是,如果未禁用元素,则需要禁用该元素。但为什么要检查呢?如果未禁用该元素,则将禁用该元素,如果该元素已被禁用,则它将保持禁用状态。那么,为什么你需要这个检查。此外,检查不是很正确。首先,你需要确保 v 不是null或 undefined ,这是我在第一段中解释的原因,但是假设您已经调试了代码并确保它是一个有效的输入元素。您已经知道 v.disabled 为false。但是,检查(!v.disabled)是否足够。我只是用你的例子,事实上,整个检查毫无意义。此外,你需要知道如果返回不同的操作数,并了解==,!==,===和!==运算符的真实含义: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Comparison_operators [ ^ ]。



让我们看看......你的第一个问题是2014年7月。即使是时候你开始学习编程,这次也足以学习编程基础知识。是的,你仍然是一个初学者,但不是这个事实可以用作借口的程度。 :-)



-SA


 

(document).ready(function(){


In my thread: JavaScript - Set control's disabled not successful[^], I reported a unsuccessful in setting a control's disability dynamically. When debugging on the code, the control value is correct. However, the control does not turn to gray color as its disabled=true. I feel that in somewhere in my HTML, there is something incorrect. How can this error be detected? Thanks.

function validate_Panel7() {
    debugger;
    var v = document.getElementById('btnValidate7');
    if (v.disabled == false)
        v.disabled = true;
    else
        return;
 }

解决方案

It should work, in a way, on certain conditions.

It may not work by different reasons. For example, document.getElementById can return null because the element may not be found; "btnValidate7" should be the value of the attribute id of wanted element, moreover, all id values should be unique in the context of the whole page. One invalid id value can make the whole page incorrect. In this case, here is what happens next: v.disabled tries to refererence v, which is null, which in turn throws an exception which you simply don't see because you are not handling exceptions. In the browsers, unhandled JavaScript exceptions are blocked on top; they are left unnoticed.

In other cases, it may happen that the found element is not an input element; with such elements, you cannot use disabled. Also, I cannot see where this function is called; I don't know if it is called at all or on what stage of execution it is called. What if the element is not yet created or not rendered?

Why, after all, you put this debugger statement? If you need to debug code, actually debug it. You will see all values of all object on each step.

Here is a complete minimal HTML with script for you to compare; it does work:

<html>
    <head>
        <title>Test disabled</title>
    </head>
<body>

<button id="btnValidate7">Test</button>

<script>
function validatePanel() {
    var v = document.getElementById('btnValidate7');
    v.disabled = true;
}
validatePanel();
</script>

</body>
</html>


This is not the biggest problem you have. Much bigger problem is how you do programming (and ask questions) in general. For example, look at your "if". Probably, you wanted to disable an element if it is not disabled. But why checking for it? If the element is not disabled, it will be disabled, if it is already disabled, it will remain disabled. So, why would you need this check. Besides, the check is not really correct. First, you need to make sure v is not null or undefined, by the reasons I explained in first paragraph, but let's say you already debugged the code and made sure it is a valid input element. You already know that v.disabled is true of false. But then it would be enough to check if (!v.disabled). I'm just using your example, in fact, the whole check in pointless. Besides, you need to know what if returns for different operands, and learn about real meaning of ==, !==, === and !== operators: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Comparison_operators[^].

Let's see… Your first question if of July 2014. Even if it was time you started to learn programming, this time would be enough to learn programming basics. Yes, you are still a beginner, but not to the extent where this fact can be used for excuses. :-)

—SA



(document).ready(function() {


这篇关于JavaScript:禁用按钮不会变灰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆