无法读取null

问题描述

<div class="input-group col-md-6">
  <select id="select-mesaj-gonderim-turleri" class="form-control mesajgonderimturusec input-circle" asp- 
     for="Model.FKMesajGonderimTurleriID" asp-items="ViewBag.MesajGonderimTurleri" required>
  </select>
</div>
<div class="input-group col-md-6">
   <select id="select-birimler" class="form-control birimsec input-circle" asp-for="Model.FkBirimID" asp- 
         items="ViewBag.Birimler" required></select>
</div>

<script>
function GetBirimGetir() {
            var url = '@Url.Action("GetBirimlerByBirimKod","Helper",new { area = "Yonetim" })';
            console.log("url",url);
            LoadSelectList(url,null,'Seçiniz','select-birimler',false,('@ViewBag.GuncellenecekBirimID' != null && '@ViewBag.GuncellenecekBirimID' > 0) ? '@ViewBag.GuncellenecekBirimID' : null,'Birim Bulunamadı');
        };
        function GetMesajGonderimTurleriniGetir() {
            var url = '@Url.Action("GetMesajGonderimTurleriByID",'select-mesaj-gonderim-turleri',('@ViewBag.GuncellenecekMesajGonderimTurleriID' !== null && '@ViewBag.GuncellenecekMesajGonderimTurleriID' > 0) ? '@ViewBag.GuncellenecekMesajGonderimTurleriID' : 'null','Tür Bulunamadı');
        };

        $(document).ready(function () {
            $(document).load('change','.birimsec',GetBirimGetir);
            $(document).load('change','.mesajgonderimturusec',GetMesajGonderimTurleriniGetir);

        });
</script>

我正在使用loadselectlist javascript方法填充下拉列表

进入插入页面没有问题。下拉列表已满。我可以选择并保存。进入更新页面时,它会引发下图所示的错误

进入插入页面没有问题。下拉列表已满。我可以选择并保存。进入更新页面时,它会引发下图所示的错误

enter image description here

function LoadSelectList(url,value,label,target,callback,selectAll,selecteds,actionEmptyLabel) {
    debugger;
    var targetDropDown = $('#' + target);
    console.log("actionEmptyLabel",actionEmptyLabel);
    
    $.getJSON(url,function (result) {

        if (result.Action != GLOBAL_VARIABLES.ActionSuccess) {
            Warning(result.Message);
            return false;
        }

        var items = result.Data;

        targetDropDown.empty();
        targetDropDown.removeAttr('disabled');
        
        if (items !== null && items.length > 0) {

            if (label !== null && label !== undefined) {
                targetDropDown.append($('<option/>',{
                    value: '',text: label
                }));
            }
            console.log(" $(this)",$(this));
            console.log(" targetDropDown",targetDropDown);
            $.each(items,function (index,item) {
                console.log("index");

                if (item.disabled) {

                    targetDropDown.append($('<option/>',{
                        value: item.Value,text: item.Text,disabled: "disabled"
                    }));
                } else {
                    targetDropDown.append($('<option/>',text: item.Text
                    }));
                }
            });
        } else {
            if (actionEmptyLabel !== null && actionEmptyLabel !== undefined) {
                targetDropDown.append($('<option/>',text: actionEmptyLabel
                }));
            }
        }

        if (selectAll) {
            var selected = [];
            targetDropDown.find("option").each(function (i,e) {
                selected.push($(e).attr("value"));
            });
            targetDropDown.val(selected).trigger("change");
        }

        if (selecteds) {
            targetDropDown.val(selecteds).trigger("change");
        }
    }).fail(function (xhr,status,error) {
        console.error(xhr);
        console.error(error);

    });
}

解决方法

问题是由document.ready()和window.load()的差异引起的

document.ready()DOM对象在加载后立即执行,document.ready()不会等待图像和CSS文件加载。在加载所有图像css之后,将运行Window.load()。当我们想要获取图像的实际大小时,我们需要使用window.load()方法。

我如下更改了代码,问题得以解决。

$(document).ready(function () {
     $(window).load('change','.birimsec',GetBirimGetir());
     $(window).load('change','.mesajgonderimturusec',GetMesajGonderimTurleriniGetir());
});
,

尝试将模态附加到当前文档时出现相同的错误消息

$.get(url).done(function (data) {
   $(document).append(data).find('.modal').modal('show');
});

问题是不能附加到整个页面,而只能附加到正文

$.get(url).done(function (data) {
   $(document.body).append(data).find('.modal').modal('show');
});