jQuery to retrieve and set selected option value of html select element

前端 未结 9 1361
醉梦人生
醉梦人生 2020-11-28 21:15

I am attempting to retrieve and set the selected value of a select element (drop down list) with jQuery.

for retrievel i have tried $(\"#myId\").find(\':select

9条回答
  •  独厮守ぢ
    2020-11-28 22:03

    The way you have it is correct at the moment. Either the id of the select is not what you say or you have some issues in the dom.

    Check the Id of the element and also check your markup validates at here at W3c.

    Without a valid dom jQuery cannot work correctly with the selectors.

    If the id's are correct and your dom validates then the following applies:

    To Read Select Option Value

    $('#selectId').val();
    

    To Set Select Option Value

    $('#selectId').val('newValue');
    

    To Read Selected Text

    $('#selectId>option:selected').text();
    

提交回复
热议问题