为什么JQuery不更新隐藏字段

时间:2014-09-23 16:00:32

标签: jquery html css asp.net

我在UpdatePanel中有一些DIV:

<asp:UpdatePanel runat="server" ID="upAddDays" UpdateMode="Conditional">
    <ContentTemplate>
        <div class="addDaysSection">
            <div class="leftdiv2">Task Name: </div>
            <div class="rightdiv2" style="overflow: hidden;">
                <asp:Label ID="lblTName" runat="server" Text="" ClientIDMode="Static"></asp:Label>
            </div>
        </div>
        <div class="addDaysSection">
            <div class="leftdiv2">Current Due Date: </div>
            <div class="rightdiv2">
                <asp:Label ID="lblCurrDD" runat="server" Text="" ClientIDMode="Static"></asp:Label>
            </div>
        </div>
        <div class="addDaysSection" style="padding: 10px 0 10px 0; height: 45px;">
            <div class="leftdiv2">Add Days to Extend: </div>
            <div class="rightdiv2">
                <div style="float: left; text-align: right; padding-right: 5%; height: 100%;">
                    <span style="display: inline-block; height: 100%; vertical-align: middle;"></span>
                    <img id="imgMinus" src="../theImages/addDaysMinus_OFF.png" style="vertical-align: middle; width: 35px; height: 35px; display: none;" />
                </div>
                <div style="float: left; text-align: center;">
                    <span style="display: inline-block; height: 100%; vertical-align: middle;"></span>
                    <input type="text" value="0" id="txtExtend" maxlength="3" placeholder="Number of Days" size="3" class="txtExtend" />
                </div>
                <div style="float: left; text-align: left; padding-left: 5%; height: 100%;">
                    <span style="display: inline-block; height: 100%; vertical-align: middle;"></span>
                    <img id="imgPlus" src="../theImages/addDaysPlus_OFF.png" style="vertical-align: middle; width: 35px; height: 35px;" />
                </div>
            </div>
        </div>
        <div class="addDaysSection">
            <div class="leftdiv2">New Due Date: </div>
            <div class="rightdiv2">
                <asp:Label ID="lblNewDD" runat="server" Text="" ClientIDMode="Static"></asp:Label>
            </div>
        </div>
    </ContentTemplate>
</asp:UpdatePanel>

在UpdatePanel之外的页面上的其他位置隐藏字段:

<input type="hidden" id="newDD" name="newDD" runat="server" />

JavaScript的:

$("body").on('click', "#imgPlus", function (e) {
    //alert("plus");
    var vDays = $("#txtExtend").val();
    $("#txtExtend").val(parseInt(vDays) + 1);
    vDays = $("#txtExtend").val();
    if (vDays > 0) {
        $("#imgMinus").show();
    }
    else {
        $("#imgMinus").hide();
    }
    var date = new Date($("#lblCurrDD").text());
    var days = parseInt($("#txtExtend").val(), 10);

    if (!isNaN(date.getTime())) {
        date.setDate(date.getDate() + days);

        $("#lblNewDD").text(date.toInputFormat());
        alert($("#lblNewDD").text()); //displays: date
        $("#newDD").val($("#lblNewDD").text());
        alert($("#newDD").val()); //displays: undefined
    }
    else {
        alert("Invalid Date");
    }
});

$("body").on('click', "#imgMinus", function (e) {
    //alert("minus");
    var vDays = $("#txtExtend").val();
    if (vDays > 0) {
        $("#txtExtend").val(parseInt(vDays) - 1);
    }
    var vDays = $("#txtExtend").val();
    if (vDays <= 0) {
        $("#imgMinus").hide();
    }
    else {
        $("#imgMinus").show();
    }
    var date = new Date($("#lblCurrDD").text());
    var days = parseInt($("#txtExtend").val(), 10);

    if (!isNaN(date.getTime())) {
        date.setDate(date.getDate() - days);

        $("#lblNewDD").text(date.toInputFormat());
        alert($("#lblNewDD").text()); //displays: date
        $("#newDD").val($("#lblNewDD").text());
        alert($("#newDD").val()); //displays: undefined
    }
    else {
        alert("Invalid Date");
    }
});

为什么不使用日期更新隐藏值,而是显示undefined

1 个答案:

答案 0 :(得分:4)

因为当您将元素设置为runat="server"时,ASP.NET会更改其id,因此您必须通过.ClientID属性引用元素的生成ID:

$("#<%=newDD.ClientID %>").val()