【问题标题】:.Val() set value, but not show into TextBox.Val() 设置值,但不显示到 TextBox
【发布时间】:2016-11-30 13:02:45
【问题描述】:

所以,我有一个函数 un JS 的问题,如何调用 Ajax 请求:

function editDataAddress(idPartnerAddress) {
        // simplified
        var url = "@Url.Action("SelectAddressToEdit", "Partners", idPartnerAddress)"
        $.ajax({
            url: url,
            dataType: 'json',
            success: function (data) {
                var adresse = data.Address;
                var zip = data.Zip;
                var locality = data.Locality;
                var idLocalite = data.IdLocality;

                //alert("locality : "+locality);

                $("#ZipSwiss").empty()
                $("#ZipSwiss").append($('<option></option>').val(idLocalite).html(zip));

                
                $('#PartnerAdresse_Locality').val(locality)
              // This line confirm i don't have another PartnerAdresse_Locality in my page
                console.log('ID Test:', $('[id=PartnerAdresse_Locality]').length, $('[id=PartnerAdresse_Locality]').get())

            }
        });
    }
<div class="col-md-4" id="divZipSwiss">
@Html.DropDownListFor(model => model.ZipSwiss, ViewBag.localiteList as IEnumerable<SelectListItem>, "Recherche...", new {@class = "form-control", style = "width : 100%;"})
</div>
  
<div class="col-md-5">
@Html.TextBoxFor(model => model.PartnerAdresse.Locality, new { @class = "form-control" })
</div>

问题是,当我调用函数时,ajax 返回所有值,并正确放入 4 first var (adresse, zip, locality, idlocalite)

当我执行$('#PartnerAdresse_Locality').val(locality) 时,该值在 HTML 中设置,但不显示: 和 HTML:

<input class="form-control" id="PartnerAdresse_Locality" name="PartnerAdresse.Locality" type="text" value="Geneva">

NPA(Zip)没问题,locality值填了,但不显示

如果我现在发表评论 $("#ZipSwiss").append($('&lt;option&lt;/option&gt;').val(idLocalite).html(zip)); 地点出现了,但当然,由于评论,不再是 NPA (zip)。

我真的迷路了,有人可以帮助我吗?

【问题讨论】:

  • 您在附加选项之后缺少结束标签 >。这是您的代码中的错字还是仅在您的问题中?
  • 谢谢,我编辑了。这是复制/粘贴错误

标签: javascript jquery ajax asp.net-mvc select2


【解决方案1】:

根据我的解释,我通过注释一些代码行来创建解决方案:

对于我的 ZipSwiss,我有一个 select2 DDL:

$('#ZipSwiss')
            .select2({
                language: lang,
                minimumInputLength: 2,
                minimumResultsForSearch: Infinity,
                closeOnSelect: true,
                ajax: {
                    url: '@Url.Action("SearchLocalite", "Partners")',
                    dataType: 'json',
                    delay: 200,
                    data: function(params) {
                        return {
                            searchTerm: params.term
                        };
                    },
                    processResults: function(data) {
                        return {
                            results: $.map(data, function(item) {
                                return {
                                    id: item.IdLocalite,
                                    text: item.ZipCode,
                                    name: item.Name
                                }
                            })
                        }
                    },
                    cache: true
                },
                templateResult: function(item) {
                    if (item.loading)
                        return item.text;

                    return item.text + " | " + item.name;
                },
                templateSelection: function (item) {
                    $("#PartnerAdresse_Locality").val(item.name);
                    return item.text;
                },
                escapeMarkup: function(markup) {
                     return markup;
                }
            });

当您更新选择时,总是会调用该行:$("#PartnerAdresse_Locality").val(item.name);。意思是,该值为空。

谢谢^^

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 2017-11-05
    • 1970-01-01
    • 2021-06-17
    相关资源
    最近更新 更多