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

前端 未结 7 1201
我在风中等你
我在风中等你 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:51

    Your syntax is wrong.

    You need to call attr with two parameters, like this:

    $('.salesperson', newOption).attr('defaultSelected', "selected");
    

    Your current code assigns the value "selected" to the variable defaultSelected, then passes that value to the attr function, which will then return the value of the selected attribute.

提交回复
热议问题