将值设置为文本框控件

Set value to textbox control

本文关键字:控件 文本 设置      更新时间:2023-09-26

如何将年龄的值设置为文本框ID是txtage?我想将年龄设置为文本框值而不是警报框

<asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:TextBox ID="txtDate" runat="server" />
<asp:TextBox ID="txtage" runat="server" />
    <asp:ImageButton runat="server" ID="imgPopup" ImageUrl="~/Images/Calendar.png" />
    <cc1:CalendarExtender ID="CalendarExtender1" OnClientDateSelectionChanged="DateSelectionChanged"
        runat="server" TargetControlID="txtDate" PopupButtonID="imgPopup" />
    <script type="text/javascript">
        function DateSelectionChanged(e) {
            var today = new Date();
            var dob = e.get_selectedDate();
            var months = (today.getMonth() - dob.getMonth() + (12 * (today.getFullYear() - dob.getFullYear())));
            alert("Your age: " + Math.round(months / 12));
        }    
    </script>

你必须做

$('#<%=txtDate.ClientID %>').val("your value");

在 asp.net 使用客户端ID - 推荐但meh..不是那么多。我会尽量避免用javascript编写ClientID

试试这个:使用 jquery

$('[id$=txtDate]').val('your value');

或使用JavaScript

document.getElementById('txtDate').value='your value';