如何在DropDownList的更改上解决Javascript未捕获的referenceerror?

时间:2015-06-03 11:27:09

标签: javascript jquery asp.net

我正在尝试根据下拉列表数据的选择设置Label文本属性。我有一个标签控件存储当前日期,我从下拉列表中选择要添加当前日期的天数,并在另一个标签控件中显示新日期。这样做我得到了

  

未捕获的ReferenceError:未定义findDate。

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://cdn.jsdelivr.net/json2/0.1/json2.js"></script>
<script type="text/javascript">
    function findDate() {
    var selectvalid = $("select[id$=ddlvalid]").val();
            if (selectvalid == "Select Validity") {
                alert("Please Select Validity");
            }
            else if (selectvalid == "Upto 7 Days") {

                var today = new Date();
                var tomorrow = new Date(today);
                tomorrow.setDate(today.getDate() + 7);

                var dd = tomorrow.getDate();
                var mm = tomorrow.getMonth() + 1;
                var y = tomorrow.getFullYear();

                var output = tomorrow.getFullYear() + '/' + (mm < 10 ? '0' : '') + mm + '/' + (dd < 10 ? '0' : '') + dd;
                $("span[id$=lblenddt]").text(output);
            }
            else if (selectvalid == "Upto 15 Days") {
                var today = new Date();
                var tomorrow = new Date(today);
                tomorrow.setDate(today.getDate() + 15);


                var dd = tomorrow.getDate();
                var mm = tomorrow.getMonth() + 1;
                var y = tomorrow.getFullYear();

                var output = tomorrow.getFullYear() + '/' + (mm < 10 ? '0' : '') + mm + '/' + (dd < 10 ? '0' : '') + dd;
                document.getElementById("<%=lblenddt.ClientID%>").innerHTML = output;
            }
            else if (selectvalid == "Upto 30 Days") {
                var today = new Date();
                var tomorrow = new Date(today);
                tomorrow.setDate(today.getDate() + 30);

                var dd = tomorrow.getDate();
                var mm = tomorrow.getMonth() + 1;
                var y = tomorrow.getFullYear();

                var output = tomorrow.getFullYear() + '/' + (mm < 10 ? '0' : '') + mm + '/' + (dd < 10 ? '0' : '') + dd;
                document.getElementById("<%=lblenddt.ClientID%>").innerHTML = output;
            }
    };
        window.onload = function() {
            var d = new Date();
            var month = d.getMonth() + 1;
            var day = d.getDate();
            var output = d.getFullYear() + '/' + (month < 10 ? '0' : '') + month + '/' + (day < 10 ? '0' : '') + day;
            document.getElementById("<%=lblenqmdon.ClientID%>").innerHTML = output;
        };
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
    <td>Enquiry Made On:</td>
    <td>
        <label id="lblenqmdon" runat="server"></label>
    </td>
    </tr>
    <tr>
    <td>Enquiry Validity:</td>
    <td><asp:DropDownList ID="ddlvalid" runat="server" ClientIDMode="static" onchange="javascript:findDate();">
                                <asp:ListItem>Select Validity</asp:ListItem>
                                <asp:ListItem>Upto 7 Days</asp:ListItem>
                            <asp:ListItem>Upto 15 Days</asp:ListItem>
                            <asp:ListItem>Upto 30 Days</asp:ListItem>
                </asp:DropDownList>
                </td>
    </tr>
    <tr>
    <td>Enquiry Valid Upto:</td>
    <td>
        <label id="lblenddt" runat="server" ClientIDMode="static"></label>
    </td>
    </tr>
</table>
</asp:Content>

请指导我在哪里做错了?

1 个答案:

答案 0 :(得分:1)

只需在您编写的javascript中定义一个函数findDate $(function findDate()将其更改为function findDate(),您也不需要在已绑定此javascript代码的javascript代码中再次绑定onchange在下拉列表中显示onchange="javascript:findDate();"

function findDate() {

            var selectvalid = $("#ddlvalid option:selected").text();
            if (selectvalid == "Select Validity") {
                alert("Please Select Validity");
            }
            else if (selectvalid == "Upto 7 Days") {

                var today = new Date();
                var tomorrow = new Date(today);
                tomorrow.setDate(today.getDate() + 7);

                var dd = tomorrow.getDate();
                var mm = tomorrow.getMonth() + 1;
                var y = tomorrow.getFullYear();

                var output = tomorrow.getFullYear() + '/' + (mm < 10 ? '0' : '') + mm + '/' + (dd < 10 ? '0' : '') + dd;
                document.getElementById("<%=lblenddt.ClientID%>").value = output;
            }
            else if (selectvalid == "Upto 15 Days") {
                var today = new Date();
                var tomorrow = new Date(today);
                tomorrow.setDate(today.getDate() + 15);


                var dd = tomorrow.getDate();
                var mm = tomorrow.getMonth() + 1;
                var y = tomorrow.getFullYear();

                var output = tomorrow.getFullYear() + '/' + (mm < 10 ? '0' : '') + mm + '/' + (dd < 10 ? '0' : '') + dd;
                document.getElementById("<%=lblenddt.ClientID%>").innerHTML = output;
            }
            else if (selectvalid == "Upto 30 Days") {
                var today = new Date();
                var tomorrow = new Date(today);
                tomorrow.setDate(today.getDate() + 30);

                var dd = tomorrow.getDate();
                var mm = tomorrow.getMonth() + 1;
                var y = tomorrow.getFullYear();

                var output = tomorrow.getFullYear() + '/' + (mm < 10 ? '0' : '') + mm + '/' + (dd < 10 ? '0' : '') + dd;
                document.getElementById("<%=lblenddt.ClientID%>").innerHTML = output;
            }
      }
        window.onload = function() {
            var d = new Date();
            var month = d.getMonth() + 1;
            var day = d.getDate();
            var output = d.getFullYear() + '/' + (month < 10 ? '0' : '') + month + '/' + (day < 10 ? '0' : '') + day;
            document.getElementById("<%=lblenqmdon.ClientID%>").innerHTML = output;
        };
    });

修改

您已在下拉列表中设置了ClientIDMode="static",并且可以通过精确ID找到标签