【问题标题】:jquery clone select element within a <tr><td><tr><td> 中的 jquery 克隆选择元素
【发布时间】:2016-03-01 17:37:55
【问题描述】:

我想克隆一个选择并更新 nameid 值。选择在 &lt;tr&gt;&lt;td&gt;

<table>
<tr id="tr_1">
    <td id="td_1">
        <select name="tech_1" id="tech_1">
            <option value="0">Please Select</option>
            <option value="1">Mango</option>
            <option value="2">Apple</option>
            <option value="3">Banana</option>
            <option value="4">Orange</option>
        </select>
    </td>
</tr>
</table>
<input type="button" id="btnClone" value="Clone" />

我可以在没有桌子的情况下做到这一点,但我的挑战是将克隆放入新的 &lt;tr&gt;&lt;td&gt; ... &lt;/td&gt;&lt;/tr&gt;

这是我的 jquery:

$("#btnClone").bind("click", function () {

   // get the last SELECT which ID starts with ^= "tech_"
   var $tr = $('tr[id^="tr_"]:last');

   // get the last SELECT which ID starts with ^= "tech_"
    var $select = $('select[id^="tech_"]:last');

    // Read the Number from that SELECT's ID (i.e: 3 from "tech_3")
    // And increment that number by 1
    var num = parseInt( $select.prop("id").match(/\d+/g), 10 ) +1;

    // Clone it and assign the new ID (i.e: from num 4 to ID "tech_4")
    var $klon = $select.clone().prop('id', 'tech_'+num ).prop('name', 'tech_'+num );

    // Finally insert $klon wherever you want
    $tr.after("<tr><td>").after($klon).after("</td></tr>");
});

此代码导致克隆的&lt;select&gt; 在原始下方,而新的&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt; 之间没有任何内容

【问题讨论】:

  • 您使用的是什么版本的 jQuery?这可能不是问题,但自从我看到使用bind() 以来已经有很长时间了(我建议改用on(),但这可能是我个人的熟悉和偏好)。顺便说一句,您使用所有这些id 属性吗?看来你让自己的生活变得困难了。顺便说一句,你的按钮在哪里? &lt;table&gt; 之外的某个地方?您是有意克隆特定的 &lt;select&gt;,还是仅克隆 last &lt;select&gt; 元素?
  • 任何时候我看到像$("[id^=xxx_"]) 这样的选择器都表明你应该使用一个类。
  • 我已将按钮代码添加到此问题中。它在原始代码中。我想克隆最后一个 &lt;select&gt; 元素,用户可以根据需要添加任意数量的 &lt;select&gt; 元素,每个元素都有一个唯一的 ID。

标签: javascript jquery


【解决方案1】:

首先将trtd 添加到trid,然后将select 添加到新td 下的td,如下所示。

$("#btnClone").bind("click", function () {
    var $tr = $('tr[id^="tr_"]:last');
    var $select = $('select[id^="tech_"]:last');

    var num = parseInt($select.prop("id").match(/\d+/g), 10) + 1;
    var $klon = $select.clone().prop('id', 'tech_' + num).prop('name', 'tech_' + num);

    $tr.after($("<tr id=tr_" + num + "><td></td></tr>")); // change here
    $('#tr_' + num + ' td').append($klon); // change here 
});

【讨论】:

    【解决方案2】:

    在创建trtd后尝试append选择元素,

    $tr.after($("<tr><td></td></tr>").find("td").append($klon).end());
    

    【讨论】:

    • @BrianG 我们必须添加.end() 才能返回到链中。现在检查答案/
    • Firebug 在 &lt;tr&gt; 中显示 和第一个克隆的
    • 如果true没有被添加到.clone()被克隆元素的子元素将不会被克隆
    • @guest271314 我没有听说过这样的事情。如果你通过 true 那么这将被视为一个硬克隆。这样在克隆过程中也将复制与元素一起存在的数据以及与元素绑定的事件。
    • 感谢帮助。还将原来的&lt;tr&gt; 更改为&lt;tr class="tr_tech_cont"&gt; 并修改了两条jquery 行:var $tr = $('tr[class="tr_tech_cont"]:last')$tr.after($("&lt;tr class='tr_tech_cont'&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;").find("td").append($klon).end())
    【解决方案3】:

    您应该创建元素而不是附加 html。

    var $klon = $select.clone().prop('id', 'tech_'+num ).prop('name', 'tech_'+num );
    var newTr = $('<tr/>');
    var newTd = $('<td/>');    
    
    newTr.insertAfter($tr);        
    newTr.append(newTd);
    newTd.append($klon);
    

    工作示例:https://jsfiddle.net/DinoMyte/raz34m93/

    【讨论】:

      【解决方案4】:

      尝试使用jQuery(html, attributes) ;另外,要包含克隆元素的 childNodes,请将 true 添加到 .clone(true) ,请参阅 .clone()

      $tr.after($("<tr>", {append:$("<td>", {append:$klon})}))
      

      $("#btnClone").on("click", function() {
      
        // get the last SELECT which ID starts with ^= "tech_"
        var $tr = $('tr[id^="tr_"]:last');
      
        // get the last SELECT which ID starts with ^= "tech_"
        var $select = $('select[id^="tech_"]:last');
      
        // Read the Number from that SELECT's ID (i.e: 3 from "tech_3")
        // And increment that number by 1
        var num = parseInt($select.prop("id").match(/\d+/g), 10) + 1;
      
        // Clone it and assign the new ID (i.e: from num 4 to ID "tech_4")
        var $klon = $select.clone(true).prop('id', 'tech_' + num).prop('name', 'tech_' + num);
      
        // Finally insert $klon wherever you want
        $tr.after(
          $("<tr>", {
            append: $("<td>", {
              append: $klon
            })
          })
        )
      });
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      <table>
        <tr id="tr_1">
          <td id="td_1">
            <select name="tech_1" id="tech_1">
              <option value="0">Please Select</option>
              <option value="1">Mango</option>
              <option value="2">Apple</option>
              <option value="3">Banana</option>
              <option value="4">Orange</option>
            </select>
          </td>
        </tr>
      </table>
      
      <button id="btnClone">click</button>

      【讨论】:

      • {html:$("&lt;td&gt;", {html:$klon} 这不起作用。因为它类似于.html($klon)
      • @RajaprabhuAravindasamy “这行不通。” ?请参阅帖子中的 stacksn-ps
      • If true is not added to .clone() children of cloned element will not be cloned 这是你的声明。我从您的陈述中了解到,如果我们不通过 true,则不会克隆(复制)select 中的选项。但是现在你通过引用文档说user selectionstext typed 不会被复制到文本区域。
      • @RajaprabhuAravindasamy 我错了;我最初的陈述是不准确的。添加了关于 .clone()select 元素的注释以供参考
      猜你喜欢
      • 2019-06-13
      • 1970-01-01
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 2011-04-23
      • 1970-01-01
      相关资源
      最近更新 更多