简体   繁体   English

将数据加载到使用JSON返回的表单字段中时出现问题

[英]Issue while loading data into form field which is returned using JSON

I have form layout where i am loading city details like city name, state and country depending on City id and is returned via JSON. 我有表单布局,其中我根据城市ID加载城市详细信息,例如城市名称,州和国家/地区,并通过JSON返回。

Function for the same as follow: 功能如下:

public JsonResult GetCityDetails(int city_id)
    {
        var result = "SELECT (CITY_NAME + ', ' + STATE_NAME + ', ' + COUNTRY_NAME) AS CITY_NAME FROM COUNTRY_MASTER cm, STATE_MASTER sm, CITY_MASTER cy where cy.STATE_ID = sm.STATE_ID AND sm.COUNTRY_ID = cm.COUNTRY_ID AND cy.CITY_ID =" + city_id + " ";
        var tempPerson = DatabaseHelper.Instance.Database.QuerySingle(result);
        return Json(new { Data = tempPerson.CITY_NAME });
    }

Here, i am getting correct data for city details which is returned via JSON, but issue while loading into html form field. 在这里,我正在获取通过JSON返回的城市详细信息的正确数据,但在加载到html表单字段时会出现问题。

However, this function is called in java script function as follow: 但是,此函数在Java脚本函数中按如下方式调用:

loadCityDetails: function (_cityid) {
           debugger;
            console.log("loadCityDetails");
            $.ajax({
                url: '@Url.Action("GetCityDetails", "Yogya")' + '?address_id=' + currentAddressID + '&person_id=' + currentPersonID + '&city_id=' +_cityid,
                type: 'POST',
                success: function (data) {
                 alert('Address Selected');                      
                   $("#CITY_DETAIL_AC").val(data.Data);

                }
            });
        },

Now, issue is that if i don't keep alert('Address Selected'); 现在,问题是,如果我不保持警惕(“选定的地址”); code in loadCityDetails, i am not getting city details loaded into city name field in form it comes only if alert is provided. 在loadCityDetails中的代码中,我不会将城市详细信息以仅在提供警报的情况下以形式出现在城市名称字段中。 But i don't want to keep this alert. 但是我不想保持这种警觉。 So, how can i get city details loaded without displaying alert. 因此,如何在不显示警报的情况下加载城市详细信息。 Any help will be appreciated. 任何帮助将不胜感激。

It's because the call to $("#CITY_DETAIL_AC").val(data.Data); 这是因为对$("#CITY_DETAIL_AC").val(data.Data); happens in an async ajax-call, which runs independently of the DOM construction. 发生在异步ajax调用中,该调用独立于DOM构造运行。 You have no way of knowing if the DOM is ready by the time the function call returns - it probably isn't, since you can't access the element. 您无法确定函数调用返回时DOM是否已准备就绪-可能还没有,因为您无法访问该元素。 Displaying the alert-box gives the system time to create the DOM before trying to write to it. 显示警报框使系统有时间在尝试写入DOM之前创建它。

Solution: Put the function containing the ajax call in .ready(), like this: 解决方案:将包含ajax调用的函数放在.ready()中,如下所示:

$(document).ready(function(){..your function body here...});

Then it's guaranteed not to happen before all elements are present. 然后保证不会在所有元素都出现之前发生。 Now you will have to retrieve city-id from within the {...function body...}, as you can't expect it passed in as an argument anymore. 现在,您将不得不从{... function body ...}中检索city-id,因为您不能期望它再作为参数传递进来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM