选择城镇时如何自动填充其他字段

时间:2015-08-14 09:25:24

标签: javascript jquery asp.net auto-populate

我有一个表格中的3个文本框 - 城镇,县和国家。将显示城镇列表,以便用户可以从此列表中进行选择。选择城镇时,我希望填充国家和县文本框。但是当选择城镇时,其他文本框中没有任何内容。

 function OnCheckIfTownValid<%= AddressType %>(result) {
        if(result.ID != -1){
            document.getElementById('<%= txtAddTownID.ClientID %>').value = result.ID;
            document.getElementById('<%= txtAddTown.ClientID %>').value = result.TownName;
            $('#<%= txtCountyID.ClientID %>').val(result.CountyID);
            $('#<%= txtCountryID.ClientID %>').val(result.CountryID);
            $('#<%= txtCounty.ClientID %>').val(result.County);
            $('#<%= txtCountry.ClientID %>').val(result.Country); 
        }
        else{
            document.getElementById('<%= txtAddTownID.ClientID %>').value = "0";
            document.getElementById('<%= txtAddTown.ClientID %>').value = "";
        }

这是console.log返回的内容:

[object Object] {CountryID: 529, CountyID: 11852, CreatedBy: "", CreatedDate: date, Element: null, ID: 437, IsActive: true, ModifiedBy: "", ModifiedDate: date, TownISO: "0", TownName: "Aberdeen", Zone: ""}
   {
      [functions]: ,
      __proto__: { },
      __type: "Lookups+Town",
      CountryID: 529,
      CountyID: 11852,
      CreatedBy: "",
      CreatedDate: [date] Thu Aug 13 2015 17:13:53 GMT+0100 (GMT Daylight Time),
      Element: null,
      ID: 437,
      IsActive: true,
      ModifiedBy: "",
      ModifiedDate: [date] Thu Aug 13 2015 17:13:53 GMT+0100 (GMT Daylight Time),
      TownISO: "0",
      TownName: "Aberdeen",
      Zone: ""
   }

1 个答案:

答案 0 :(得分:0)

知道了!

试试这个:

function OnCheckIfTownValid<%= AddressType %>(result) {
        if(result.ID != -1){
            document.getElementById('<%= txtAddTownID.ClientID %>').value = result["ID"];
            document.getElementById('<%= txtAddTown.ClientID %>').value = result["TownName"];
            $('#<%= txtCountyID.ClientID %>').val(result["CountyID"]);
            $('#<%= txtCountryID.ClientID %>').val(result["CountryID"]);
            $('#<%= txtCounty.ClientID %>').val(result["County"]);
            $('#<%= txtCountry.ClientID %>').val(result["Country"]);
        }
        else{
            document.getElementById('<%= txtAddTownID.ClientID %>').value = "0";
            document.getElementById('<%= txtAddTown.ClientID %>').value = "";
        }

您还可以在函数中放置Console.Log(Result),以便查看是否填充了结果。

修改:链接更多信息:Link here