简体   繁体   中英

get data from select dropdown menu

I am using http://designwithpc.com/Plugins/ddSlick#demo . A plugin that customizes dropdown menus with images, texts etc. Am trying to get the text when i select one of the select option from the dropdown but my browsers console log says undefined. I have tried placing the data into a variable on line 7 but it still gives me the same outcome.

$('#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);
    }   
});

This is the structure of the 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"
}];

I got data by Json Object.

earlier it returns only [object 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>

But as per my firebug it shows my json response like this

[
   {
      "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"
   }
   ]

then I changed it like this..

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

to

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

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