【问题标题】:Javascript - Cannot read property 'value' of null - Combobox valueJavascript - 无法读取 null 的属性“值” - 组合框值
【发布时间】:2019-05-24 23:07:32
【问题描述】:

想知道是否有人可以帮助使用以下代码。我有一个名为 "cboAgreement" 的组合框,它是一个下拉列表,包含两个值 "Agree""Disagree"

我有一个表单,cmets 网格最初是隐藏的,但是当用户选择"Disagree" 时,cmets 网格应该出现。

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

“未捕获的类型错误:无法读取 null 的属性‘值’”

我尝试将 if 语句更改为 decision.getvalue(),但同样的事情。

<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% 做错了!

提前感谢您的帮助。

【问题讨论】:

  • 这意味着它找不到document.getElementById("cboAgreement") 我打赌您的服务器端框架正在更改 id 或者您在页面上存在之前引用了元素。
  • 这可能是你的第二点。我在Sys.Application.add_load(FormLoad) function FormLoad() { HideComments(true); Decision(true); } 中添加了Decision 函数有我上面显示的代码,但我仍然收到错误。
  • commentsGrid.style.visibility = "hidden"; 工作正常,这是一个 div 的 id。我不认为 ID 正在更改。
  • 查看页面源码,不难分辨。

标签: javascript combobox sequence


【解决方案1】:

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

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

还可以查看此快速教程: https://idiallo.com/javascript/uncaught-typeerror-cannot-read-property-of-null

【讨论】:

  • 其实我可能做错了。这会返回一个错误:var decision = document.getElementById("cboAgreement").value; if (decision) { decision = decision.value; }
  • 在检查是否存在之前,您不应该获取元素的值,从 var deceleration 中删除 .value。
  • 好的。我这样做了,检查菜单中没有错误。不确定下一步该做什么?
  • 我认为这可能与“comboboxItem”部分有关,它似乎没有找到项目。
  • 你能帮忙吗?
猜你喜欢
  • 2019-09-17
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
相关资源
最近更新 更多