简体   繁体   中英

How to get selected item in dropdown menu

I need to get a selected item from drop menu,

I am using this script: LINK This is my code I just need to get values in javascript:

function checkData() {
    var pagesObj = document.getElementById("website2"); 
    alert(pagesObj.options[pagesObj.selectedIndex].value);
}

$(document).ready(function() {
    $.ajax({
        url: "get_data.php",
        cache: false,
        dataType: 'json',
        data: {},
        success: function(data) {
            for (var i = 0; i < data.results.length; i++) {
                if(data.results[i].value != '0' ) {
                    oHandler = $("#websites2").msDropDown().data("dd");
                    oHandler.add({text:'', value:'', title:''});
                    oHandler.add({text:data.results[i].text,value:data.results[i].value,title:data.results[i].title});
                }
             }
         }
    });
});

This checkData() function is giving me error that option is not defined and it is null

EDIT:

Html:

<select name="websites2" id="websites2" onChange="checkData()" style="width:200px;"tabindex="1"></select>

I believe it is as simple as this (using jQuery):

var selectedIndex = $("#websites2").val();

Since you have jQuery

$('#websites2').val()

should do it, though I've found that a bit unreliable on Opera just recently. The following works reliably for me on all the browsers I've tested:

$('#websites2 option:selected').val()

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