如何在Content Place Holder中使用JavaScript设置Label文本属性?

时间:2015-05-26 11:22:57

标签: javascript jquery html asp.net

我有一个带有两个html标签控件和一个asp.net下拉列表的表单。 Label1显示当前日期和下拉列表数据选择我试图设置到期日期并在Label2中显示它。我能够在普通的html页面中执行此操作,但是当放置在ContentPlaceHolder中时,我的javascript无法正常工作。请提供指导 -

我的aspx页面

<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 () {
        $("#ddlvalid").change(function () {
            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').innerHTML = 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').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').innerHTML = output;
            }
        });

        window.onload = function show() {
            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').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" 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"></label>
    </td>
    </tr>
</table>
</asp:Content>

2 个答案:

答案 0 :(得分:0)

我认为您的Label ID在JavaScript中不起作用。

document.getElementById("<%=lblenddt.ClientID%>").innerHTML = output;
document.getElementById("<%=lblenqmdon.ClientID%>").innerHTML = output

答案 1 :(得分:0)

你的jquery库在哪里?它可能是关于你放置jquery lib的地方。确保它放在此代码之前