Javascript-无法读取null的属性“值”-组合框值 [英] Javascript - Cannot read property 'value' of null - Combobox value

查看:76
本文介绍了Javascript-无法读取null的属性“值”-组合框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道是否有人可以帮助以下代码。我有一个名为 cboAgreement 的组合框,它是一个带有两个值 Agree 不同意 。

Wondering if anyone could help with the following code. I have a combobox called "cboAgreement" which is a dropdown with two values "Agree" and "Disagree".

我有一个表单,其中的注释网格最初是隐藏的,但是当用户选择不同意 应该会出现评论网格。

I have a form where, the comments grid is initially hidden, but when a user chooses "Disagree" the comments grid should appear.

当我检查网页时,它会返回此错误:

When i inspect the webpage, it gives back this error:


Uncaught TypeError:无法读取null的属性值

"Uncaught TypeError: Cannot read property 'value' of null"

我尝试将if语句更改为决策.getvalue(),但还是一样。

I have tried changing the if statement to decision.getvalue() but again same thing.

<script type="text/javascript">

  var commentsGrid = document.getElementById("comms");
  var decision = document.getElementById("cboAgreement").value;

  commentsGrid.style.visibility = "hidden";


  if (decision == "Disagree") {
  commentsGrid.style.visibility = "visible";
  }else{
  commentsGrid.style.visibility = "hidden";
  }


</script>

,组合框如下:

<sq8:ComboBox runat="server" ID="cboAgreement"><Items>
<sq8:ComboBoxItem runat="server" Text="Agree" Selected="True"></sq8:ComboBoxItem>
<sq8:ComboBoxItem runat="server" Text="Disagree"></sq8:ComboBoxItem>
</Items>
</sq8:ComboBox>
<sq:BindableControl runat="server" TargetControlID="cboAgreement" DataField="cboAgreement"></sq:BindableControl>

我在做什么错?我应该说,我是JavaScript新手,所以99.9%的人做错了事!

What am i doing wrong? I should say, I'm new to JavaScript so 99.9% doing something incorrect!

在此先感谢您的帮助。

推荐答案

这意味着您正在尝试访问未定义对象的属性,换句话说,您试图获取
仍然是有价值的东西空的
这通常发生在我们在使用对象之前不对其进行测试的情况下。
我建议在获取元素值之前检查该元素是否存在。

It means that you are trying to access a property of an object that is undefined, in other words you are trying to get it is value while it is still empty This usually happens when we don't test an object before using it. I recommend checking the element exists before getting it is value.

   if (decision) {
      decision = decision.value; 
   }

也请查看此快速教程:
https://idiallo.com/javascript/uncaught-typeerror-cannot-read-property-空值

Also check this quick tutorial: https://idiallo.com/javascript/uncaught-typeerror-cannot-read-property-of-null

这篇关于Javascript-无法读取null的属性“值”-组合框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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