【问题标题】:jQuery append <option> to appended <select>jQuery 将 <option> 附加到附加的 <select>
【发布时间】:2014-07-25 11:30:18
【问题描述】:

我正在尝试将选项标签附加/附加到表单中附加的选择输入中。 只是我的表单像这样附加到正文中:

c.append( $('<form>').attr({
  'method': 'POST',
  'action': ''
}).append( $('<select />').attr({
     'name': 'retour'
}) ));

现在我想在此表单中附加选项标签,而不必像 5 次那样创建 .append( $('&lt;option&gt;').attr() ) 等。

我发现了以下问题:

  1. jQuery append inside appended element
  2. What is the best way to add options to a select from an array with jQuery?
  3. Programmatically create select list

但它们都不适用于我的情况。 希望有人知道答案,谢谢。

【问题讨论】:

    标签: jquery select append option appendto


    【解决方案1】:

    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'})
            )
        )
    );
    

    FIDDLE

    【讨论】:

    • 太棒了,谢谢。但是如果我想要一个数组(比如 JSON)到
    • 不确定你的意思,但我猜你会遍历数组并独立创建每个选项并附加到选择等。
    • 我想附加
    猜你喜欢
    • 1970-01-01
    • 2010-12-03
    • 2018-12-22
    • 2013-05-21
    • 2017-10-28
    • 2012-05-24
    • 2016-01-23
    • 2017-11-14
    • 2017-10-18
    相关资源
    最近更新 更多