2015-08-15 19 views
0

我有問題來設置貨幣texboxt在下拉初始加載時,它只會在下拉更改後更改,下拉列表填充數據庫使用codebehind c#和填充下拉改變我用ajax設置TextBox基於來自DropdownList的值(由數據庫填充)在初始加載

以及這裏的代碼 下拉

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

和功能ddlPaymentCurrency平變化

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; 
     } 

任何人都可以提供幫助嗎?

+0

在下拉列表中是否有默認值? –

+0

沒有默認值的下拉菜單 – Ana

+0

那麼你想顯示什麼樣的價值而不需要更改? –

回答

0

這是怎麼了,你就會有選擇的下拉菜單的第一個項目的價值,並在文中使用JQuery設置文本框的值框。使用jQuery。

$(document).ready(function(){ 

$('#ddlPaymentCurrency').val($('#ddlPaymentCurrency option:first-child').val()); 

$('#yourTextBoxId').val($('#ddlPaymentCurrency').val()); 

}); 
+0

您是否嘗試過答案??? –

0

您可以根據網頁加載的下拉列表值,或者在前端

+0

,對不起,您是否在頁面加載中嘗試過? 我已經嘗試在頁面加載時使用selectedValue獲取該值,在頁面加載時,下拉列表沒有選定值。 。如果使用JQuery,但仍然混淆瞭如何在沒有變化的情況下在dropdownlist中獲取它的值 – Ana

相關問題