javascript错误:无法读取null的属性"options" [英] javascript error: Cannot read property 'options' of null

查看:126
本文介绍了javascript错误:无法读取null的属性"options"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个GridView,它的一列带有一个文本框,用户可以在其中输入一个值,而一列则带有一个下拉列表.如果用户在文本框中输入不等于1的值,则他们必须从下拉列表中选择一个值. DDL中的默认值是选择",它只是在ddlReasons.Items.Insert(0, new ListItem("Select"));中编码的空值. DDL是动态创建的,但select始终是默认值.

I have a GridView that has a column with a textbox where the user can enter a value and a column with a dropdownlist. If the user enter a values that is not equal to 1 in the textbox, they must select a value from the dropdownlist. The default value in the DDL is "Select" which is just an empty value that was coded in: ddlReasons.Items.Insert(0, new ListItem("Select"));. The DDL is created dynamically but select will always be the default value.

 function UpdateSerialQtyRcvd(SerNoID, QtyRcvd) {
        if (QtyRcvd != 1) {
            var ddl = document.getElementById("ddlReasons");
            var selectedValue = ddl.options[ddl.selectedIndex].value;
            if (selectedValue == "Select") {
                alert("Must select reason");
            }

        }
        else {
            PageMethods.UpdateSerialQtyRcvdUserControl(SerNoID, QtyRcvd, OnUpdateSuccess, OnUpdateFail);
        }
    }

如果用户输入的值不为1,那么我需要检查DDL中的值.如果"Select"是值,则我需要用户在DDL中选择其他内容,但是我在行var selectedValue = ddl.options[ddl.selectedIndex].value;

If the user enters a value that is not 1 then I need to check what value is in the DDL. If "Select" is the value I need the user to select something else in the DDL but I am getting this error on the line var selectedValue = ddl.options[ddl.selectedIndex].value;

未捕获的TypeError:无法读取null的属性选项"

Uncaught TypeError: Cannot read property 'options' of null

下拉列表代码:

<asp:TemplateField HeaderText="Reason">
 <ItemTemplate>
  <asp:DropDownList ID="ddlReasons" runat="server" class="ReasonDDL" ></asp:DropDownList>
 </ItemTemplate> 
</asp:TemplateField>

推荐答案

您需要获取 ClientId ...

JavaScript :

var ddl = document.getElementById("<%=ddlReasons.ClientID%>");

JQuery :

var ddl = $('#<%=ddlReasons.ClientID%>');

查看 MSDN documentation 有关如何访问客户端ID和其他设置的信息.

Check out the MSDN documentation on how to access the Client ID and the different settings.

这篇关于javascript错误:无法读取null的属性"options"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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