如何在开头添加选项? [英] How can I add an option in the beginning?
本文介绍了如何在开头添加选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有html选择,
<select id="myselect">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
我需要在此选择中添加新选项。
如果我这样做:
I need to add new option in this select. If I try like this:
$('#myselect').appendTo($('<option>my-option</option>'));
my-option已添加到最后
the my-option have added to the end
<select id="myselect">
<option>1</option>
<option>2</option>
<option>3</option>
<option>my-option</option>
</select>
如何在开头添加选项(作为第一个)?
喜欢这个:
How can I add an option in the beginning (as first)? Like this:
<select id="myselect">
<option>my-option</option>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
推荐答案
首先,你的意思是 追加
,而不是 appendTo
。 append
将参数的内容添加到选择中; appendTo
将选择附加到参数中指定的元素。
First, you mean append
, rather than appendTo
. append
adds the content of the parameter to the selection; appendTo
appends the selection to the element designated in the parameter.
其次,也许直观地说,你可以使用 prepend
:
Second, perhaps intuitively, you can use prepend
:
$('#myselect').prepend($('<option>my-option</option>'));
这篇关于如何在开头添加选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文