从选择下拉菜单中获取数据

get data from select dropdown menu

本文关键字:获取 数据 下拉菜单 选择      更新时间:2023-09-26

我正在使用http://designwithpc.com/Plugins/ddSlick#demo。一个插件,可以自定义带有图像、文本等的下拉菜单。当我从下拉列表中选择一个选择选项时,我试图获取文本,但我的浏览器控制台日志显示未定义。我试着将数据放入第7行的一个变量中,但它仍然给了我相同的结果。

$('#myDropdown').ddslick({
    data:ddData,
    width:300,
    selectText: "Select your preferred social network",
    imagePosition:"right",
    onSelected: function(selectedData){
        var selectedData = $('#myDropdown').data('ddslick');
        console.log(selectedData.text);
    }   
});

这是ddData:的结构

var ddData = [{
    text: "Facebook",
    value: 1,
    selected: false,
    description: "Description with Facebook",
    imageSrc: "http://dl.dropbox.com/u/40036711/Images/facebook-icon-32.png"
}, {
    text: "Twitter",
    value: 2,
    selected: false,
    description: "Description with Twitter",
    imageSrc: "http://dl.dropbox.com/u/40036711/Images/twitter-icon-32.png"
}, {
    text: "LinkedIn",
    value: 3,
    selected: true,
    description: "Description with LinkedIn",
    imageSrc: "http://dl.dropbox.com/u/40036711/Images/linkedin-icon-32.png"
}, {
    text: "Foursquare",
    value: 4,
    selected: false,
    description: "Description with Foursquare",
    imageSrc: "http://dl.dropbox.com/u/40036711/Images/foursquare-icon-32.png"
}];

我通过Json Object获取数据。

早些时候它只返回[对象对象]

<div id="myDropdown"></div>
    <script type="text/javascript">
        var jsonurl = 'dropDown.html';
        $.ajax({
            type : 'GET',
            url : jsonurl,
            data : {},
            success : function(myData) {
                $('#myDropdown').ddslick({
                    data : myData,
                    width : 300,
                    selectText : "Select the bill process",
                    imagePosition : "right",
                    onSelected : function(selectedData) {
                        alert(selectedData);
                    }
                });
            },
            error : function() {
            }
        });
    </script>

但根据我的firebug,它显示了我的json响应,如下

[
   {
      "value":1,
      "text":"Process_1",
      "selected":false,
      "imageSrc":"images//priyan.jpg",
      "description":"Process_1"
   },
   {
      "value":2,
      "text":"Process_2",
      "selected":false,
      "imageSrc":"images//priyan.jpg",
      "description":"Process_2"
   },
   {
      "value":3,
      "text":"Process_3",
      "selected":false,
      "imageSrc":"images//priyan.jpg",
      "description":"Process_3"
   }
   ]

然后我就这样改了。。

onSelected : function(selectedData) {
                        alert(selectedData);
                    }

onSelected : function(myData) {
                        alert(myData.selectedData.text);
                    }