jQuery Get Selected Option From Dropdown

前端 未结 30 3402
梦如初夏
梦如初夏 2020-11-22 02:56

Usually I use $(\"#id\").val() to return the value of the selected option, but this time it doesn\'t work. The selected tag has the id aioConceptName

30条回答
  •  故里飘歌
    2020-11-22 03:45

    Reading the value (not the text) of a select:

    var status = $("#Status").val();
    var status = $("#Status")[0].value;
    var status = $('#Status option:selected').val();
    

    How to disable a select? in both variants, value can be changed using:

    A

    User can not interact with the dropdown. And he doesn't know what other options might exists.

    $('#Status').prop('disabled', true);
    

    B

    User can see the options in the dropdown but all of them are disabled:

    $('#Status option').attr('disabled', true);
    

    In this case, $("#Status").val() will only work for jQuery versions smaller than 1.9.0. All other variants will work.

    How to update a disabled select?

    From code behind you can still update the value in your select. It is disabled only for users:

    $("#Status").val(2);
    

    In some cases you might need to fire events:

    $("#Status").val(2).change();
    

提交回复
热议问题