How do I dynamically set the selected option of a drop-down list using jQuery, JavaScript and HTML?

前端 未结 7 1197
我在风中等你
我在风中等你 2021-02-04 09:17

For some reason, I can\'t get this to work.

My options list is populated dynamically using these scripts:

function addOption(selectId, value, text, sele         


        
7条回答
  •  北荒
    北荒 (楼主)
    2021-02-04 09:53

    Here it goes.

    // Select by value
    $('select[name="options"]').find('option[value="3"]').attr("selected",true);
    
    // Select by text
    //$('select[name="options"]').find('option:contains("Third")').attr("selected",true);
    
    
    
    
    
    

提交回复
热议问题