简体   繁体   中英

object is null when setting checked state

So i have a page that has a couple of jQuery plugins. Among other things i have the multiselect toolbar, a pretty sweet plugin. problem is that when i load up the page in internet explorer the page breaks. i've been able to determine that the problem occurs when i try to set some attributes to some elements that i have dynamically generated.

here is the code for generating the elements:

$.ajax({
            url: '@Url.Content("~")' + 'Ticket/GetTvrtke',
            async: false,
            success: function (data) {
                document.getElementById("header_tvrtka_holder").innerHTML = data;

                var tvrtke = data.split(", ");

                for (var i = 0; i < tvrtke.length; i++) {
                    document.getElementById("KlijentMultiSelect").innerHTML +=
                        "<option value=\"" + tvrtke[i] + "\" id=\"" + tvrtke[i] + "\" >" + tvrtke[i] + "</option>";
                }

                $("#KlijentMultiSelect").multiselect({
                    selectedText: "",
                    height: 125,
                    minWidth: 650,
                    noneSelectedText: 'Izaberite željene tvrtke:'
                });
            }
        });

the function gets the correct data and generates the options, and then i activate the plugin to render the new dropdown menu with checkboxes.

problem is that afterwards i have this code:

           var tvrtke = document.getElementById("header_tvrtka_holder").innerHTML.split(", ");

            for (var i = 0; i < tvrtke.length; i++) {
                document.getElementById("ui-multiselect-" + tvrtke[i]).checked = true;
            }

            for (var i = 0; i < tvrtke.length; i++) {
                document.getElementById("ui-multiselect-" + tvrtke[i]).setAttribute("onclick", "ChangeTextKlijent()");
            }

here i am trying to set the checkbox values to true and add an on click event to the input element, but then visual studio sends me the error message from the title. in firefox, everything works great but IE is a whole different story.

anyone know how to fix this?

I switched so that my code looks like this:

var tvrtke = document.getElementById("header_tvrtka_holder").innerHTML.split(", ");

$.each(tvrtke, function (index, value) {
    $("#KlijentMultiSelect").append("<option value=\"" + value + "\" id=\"" + value + "\" >" + value + "</option>");
});

and now it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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