我正在尝试将选项标签 append/添加到表单中 append 的选择输入中。 只需将我的表单 append 到正文,如下所示:
c.append( $('<form>').attr({
'method': 'POST',
'action': ''
}).append( $('<select />').attr({
'name': 'retour'
}) ));
现在我想将选项标签 append 到此表单,而不必创建 .append( $('<option>').attr() )
等等5次。
我发现了这样的问题:
- jQuery append inside appended element
- What is the best way to add options to a select from an array with jQuery?
- Programmatically create select list
但它们都不适用于我的情况。 希望有人知道答案,谢谢。
最佳答案
Append 接受用逗号分隔的多个元素
c.append(
$('<form>', {
method : 'POST',
action : ''
}).append(
$('<select />', {
name : 'retour'
}).append(
$('<option />', {text : 'option1'}),
$('<option />', {text : 'option2'}),
$('<option />', {text : 'option3'}),
$('<option />', {text : 'option4'}),
$('<option />', {text : 'option5'})
)
)
);
关于jQuery 将 <option> append 到 append 的 <select>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24043806/