我在jquery中有一个动态创建的select,我想在创建之后修改第一个选项,但是在我将它附加到dom之前。
e.g
myselect = myselectcreationfuynction; :-- works fine
change value and text of first option :-- bit lost here
$('#targetdiv').html(myselect); :--works fine
答案 0 :(得分:1)
试试这段代码
$("select option:nth-child(1)").replaceWith("<option>10</option>");
修改强>
$("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>");