jQuery从JSON创建选择列表选项,不会像广告一样发生? [英] jQuery create select list options from JSON, not happening as advertised?

查看:103
本文介绍了jQuery从JSON创建选择列表选项,不会像广告一样发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何做到这一点不起作用(在空选择列表上运行< select id =requestTypes>< / select>

How come this doesn't work (operating on an empty select list <select id="requestTypes"></select>

$(function() {

        $.getJSON("/RequestX/GetRequestTypes/", showRequestTypes);

    }
    );


    function showRequestTypes(data, textStatus) {

        $.each(data,
            function() {

                var option = new Option(this.RequestTypeName, this.RequestTypeID);
                // Use Jquery to get select list element
                var dropdownList = $("#requestTypes");

                if ($.browser.msie) {
                    dropdownList.add(option);
                }

                else {

                    dropdownList.add(option, null);

                }
            }
            );

        }

但是这样做:


  • 替换:

  • Replace:

var dropdownList = $(#requestTypes);

使用简洁的javascript:

With plain old javascript:

var dropdownList = document。 getElementById(requestTypes);

推荐答案

$(#requestTypes)返回一个包含所有选定元素的jQuery对象。您尝试调用单个元素的 add()方法,而是调用 add() jQuery对象的方法,它做了很大的不同。

$("#requestTypes") returns a jQuery object that contains all the selected elements. You are attempting to call the add() method of an individual element, but instead you are calling the add() method of the jQuery object, which does something very different.

为了访问DOM元素本身,你需要将jQuery对象视为一个数组,并获得第一个通过使用 $(#requestTypes)[0]

In order to access the DOM element itself, you need to treat the jQuery object as an array and get the first item out of it, by using $("#requestTypes")[0].

这篇关于jQuery从JSON创建选择列表选项,不会像广告一样发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆