jquery访问select变量的第一个选项

时间:2014-03-12 16:54:15

标签: jquery

我在jquery中有一个动态创建的select,我想在创建之后修改第一个选项,但是在我将它附加到dom之前。

e.g

    myselect = myselectcreationfuynction; :-- works fine
    change value and text of first option :-- bit lost here
$('#targetdiv').html(myselect); :--works fine

2 个答案:

答案 0 :(得分:1)

试试这段代码

$("select option:nth-child(1)").replaceWith("<option>10</option>");

Demo

修改

$("body").append("<select><option>1</option> <option>2</option><option>3</option></select>").find("select option:eq(1)").replaceWith("<option>10</option>");

答案 1 :(得分:0)

在这一行之后:

$('#targetdiv').html(myselect);
$('#targetdiv select>option:first').replaceWith("<option>10</option>");