在初始加载时根据下拉列表中的值设置文本框(由数据库填充)

Set TextBox Based on Value From DropdownList (populated by database) on initial Load

本文关键字:置文本 填充 数据库 加载 下拉列表      更新时间:2023-09-26

我在初始加载时从下拉菜单设置货币文本框有问题,它只有在下拉菜单更改后才会更改,该下拉菜单使用c#代码填充数据库,对于下拉菜单更改我使用ajax

下面是代码下拉

 <asp:DropDownList ID="ddlPaymentCurrency" CssClass="form-control" runat="server" onblur="showCRate2()" onChange="showCRate2()" onkeyup="showCRate2()"></asp:DropDownList>

和函数ddlPaymentCurrency onchange

function showCRate2(obj) {
            this.curr();
            var selectedCurrency = $('#<%=ddlPaymentCurrency.ClientID%>').val();
            console.log(selectedCurrency);
            if (selectedCurrency != null && selectedCurrency != "") {
                $.ajax({
                    type: "POST",
                    url: "TopUp.aspx/getCurr",
                    data: '{id:"' + selectedCurrency + '"}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        var o = response.d;
                        $('#<%=hfCurrencyRate.ClientID%>').val(o.RateBuy);
                        $('#<%=hfCR.ClientID%>').val(o.RateBuy);
                    },
                    error: function (response) {
                        alert('error')
                    }
                });
            }
        }

和webmethod

[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static MukyaServiceReference.CurrRate getCurr(int id)
        {
            var CR = client.GetCurrRates(id);
            return CR;
        }

有人能帮忙吗?

这就是您如何将下拉菜单中第一项的值选中并放在文本框中。使用jquery。

$(document).ready(function(){
 $('#ddlPaymentCurrency').val( $('#ddlPaymentCurrency option:first-child').val() );
$('#yourTextBoxId').val($('#ddlPaymentCurrency').val());
});

您可以根据页面加载时的下拉列表值或在前端使用JQuery设置文本框的值