【问题标题】:How do you copy one dropdown's options to another dropdown using jquery [duplicate]如何使用 jquery 将一个下拉列表的选项复制到另一个下拉列表 [重复]
【发布时间】:2011-10-17 15:49:47
【问题描述】:

可能重复:
Copy option list from dropdownlist. JQuery

在 Jquery 中,如何将一个下拉菜单的选项复制到另一个下拉菜单?

【问题讨论】:

标签: jquery html


【解决方案1】:

我会咬人的。

$('#two').append($('#one').html());

http://jsfiddle.net/CvJbv/

【讨论】:

    【解决方案2】:

    使用 jQuery 的 .html() (http://api.jquery.com/html/) 获取第一个元素的内容,然后再次使用它来写入第二个元素的内容。像这样:

    $('#options_2').html($('#options_1).html());

    地点:

    <pre>
    <ul id="options_1">
    <li>1</li>
    <li>2</li>
    </ul>
    
    <ul id="options_2">
    <li></li>
    </ul>
    

    我也同意您应该考虑接受您以前的一些问题的答案。

    【讨论】:

    • 您好,我很抱歉,但我完全不知道如何接受答案。我确实单击了向上按钮并单击了“这篇文章对您有用吗?”。如果我遗漏了什么,请告诉我。谢谢,拉维
    • @ravi bhartiya,如果您想接受答案,可以单击上下箭头下方的小复选标记。您需要登录帐户才能执行此操作。
    • @ravi bhartiya 您也可以在stackoverflow 顶部单击您的用户名,查看您之前的所有问题、要点等。如果您找到了自己问题的答案,您可以自己发布答案并检查自己作为答案。
    【解决方案3】:

    试试这个:

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    
    <select id="select_1">
     <option>1</option>
     <option>2</option>
    </select>
    
    <select id="select_2">
     <option>A</option>
     <option>B</option>
    </select> 
    
    <button onclick="javascript:CopyLists()">Copy Lists</button>
    
    <script type="text/javascript">
     function CopyLists()
     {
      var list1 = $('#select_1');
      var list2 = $('#select_2');
      list2.find('option').remove();
      list1.children().each(function () {
        list2.append($("<option></option>").
          attr("value",$(this).val()).
          text($(this).text()));
      });
     }
    </script>
    

    【讨论】:

      【解决方案4】:

      你可以这样做:

      $('#destination').html($('#source').html());
      

      $('#destination').append($('#source').html());
      

      $('something').html() 从元素中获取 html。

      $('something').html('something') 将元素中的当前 html 替换为新的 html。

      $('something').append('something') 将新 html 放在元素中旧 html 的末尾。 (这不会取代)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多