下拉选择在克隆过程中不作为一部分。

huangapple go评论63阅读模式
英文:

dropdown select not being part upon the process of cloning

问题

克隆代码是有效的,只是下拉选择框没有被包括在克隆过程中。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
$(&quot;input.tr_clone_add&quot;).live(&#39;click&#39;, function() {
  var $tr = $(this).closest(&#39;.tr_clone&#39;);
  var $clone = $tr.clone();
  $clone.find(&#39;.form-control js-example-basic-single&#39;).val(&#39;&#39;);
  // 下面添加获取下拉选择框值的代码
  var selectValue = $tr.find(&#39;select[name="animal"]&#39;).val();
  $clone.find(&#39;select[name="animal"]&#39;).val(selectValue);
  $tr.after($clone);
});

<!-- language: lang-html -->
<table>
  <tr class=&quot;tr_clone&quot;>
    <td>
      <input type=&quot;button&quot; name=&quot;add&quot; value=&quot;clone row&quot; class=&quot;tr_clone_add&quot; />&lt;br&gt;&lt;br&gt;
      <select name=&quot;animal&quot; id=&quot;number&quot;>
        <option value=&quot;cow&quot;>cow&lt;/option&gt;
        <option value=&quot;horse&quot;>horse&lt;/option&gt;
        <option value=&quot;dog&quot;>dog&lt;/option&gt;
        <option value=&quot;cat&quot;>cat&lt;/option&gt;
      </select>
      &nbsp;
      <select name=&quot;vehicle&quot; id=&quot;letter&quot;>
        <option value=&quot;car&quot;>car&lt;/option&gt;
        <option value=&quot;ship&quot;>ship&lt;/option&gt;
        <option value=&quot;plane&quot;>plane&lt;/option&gt;
      </select>
      &nbsp;
      <input name=&quot;remarks&quot; type=&quot;text&quot;>
      <br>
      <hr>
    </td>
  </tr>
</table>
</body>

<script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js&quot;></script>

<!-- end snippet -->

我尝试的目标是,在克隆行的过程中,也将行的下拉选择框的值传递给要克隆的行。

英文:

Cloning code works it's just that the dropdown select is not being part of the cloning process.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;input.tr_clone_add&quot;).live(&#39;click&#39;, function() {
  var $tr = $(this).closest(&#39;.tr_clone&#39;);
  var $clone = $tr.clone();
  $clone.find(&#39;.form-control js-example-basic-single&#39;).val(&#39;&#39;);
  $tr.after($clone);
});

<!-- language: lang-html -->

&lt;table&gt;
  &lt;tr class=&quot;tr_clone&quot;&gt;
    &lt;td&gt;
      &lt;input type=&quot;button&quot; name=&quot;add&quot; value=&quot;clone row&quot; class=&quot;tr_clone_add&quot; /&gt;&lt;br&gt;&lt;br&gt;
      &lt;select name=&quot;animal&quot; id=&quot;number&quot;&gt;
        &lt;option value=&quot;cow&quot;&gt;cow&lt;/option&gt;
        &lt;option value=&quot;horse&quot;&gt;horse&lt;/option&gt;
        &lt;option value=&quot;dog&quot;&gt;dog&lt;/option&gt;
        &lt;option value=&quot;cat&quot;&gt;cat&lt;/option&gt;
      &lt;/select&gt;
      &amp;nbsp;
      &lt;select name=&quot;vehicle&quot; id=&quot;letter&quot;&gt;
        &lt;option value=&quot;car&quot;&gt;car&lt;/option&gt;
        &lt;option value=&quot;ship&quot;&gt;ship&lt;/option&gt;
        &lt;option value=&quot;plane&quot;&gt;plane&lt;/option&gt;
      &lt;/select&gt;
      &amp;nbsp;
      &lt;input name=&quot;remarks&quot; type=&quot;text&quot;&gt;
      &lt;br&gt;
      &lt;hr&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;

&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

What I am trying to do is, also pass the value of dropdown select of row to be cloned upon the process of cloning the row.

答案1

得分: 0

  1. **live**已被弃用,因此请更新您的jQuery库。

  2. 对于您的问题:

    1. 首先查找并获取选定的选项,然后将属性selected添加到选定的选项上。

    2. 要获取动态创建元素的事件,请使用事件委托。

示例:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
$(document).on("change", "select", function() {
  var val = $(this).val(); 
  $("option", this).removeAttr("selected").filter(function() {
    return $(this).attr("value") == val;
  }).first().attr("selected", "selected");
});
$('body').on('click', 'input.tr_clone_add', function() {
  var $tr = $(this).closest('.tr_clone');
  var $clone = $tr.clone();
  $clone.find('.form-control js-example-basic-single').val('');
  $tr.after($clone);
});

<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr class="tr_clone">
    <td>
      <input type="button" name="add" value="clone row" class="tr_clone_add" /><br><br>
      <select name="animal" id="number">
        <option value="cow">cow</option>
        <option value="horse">horse</option>
        <option value="dog">dog</option>
        <option value="cat">cat</option>
      </select>
      &nbsp;
      <select name="vehicle" id="letter">
        <option value="car">car</option>
        <option value="ship">ship</option>
        <option value="plane">plane</option>
      </select>
      &nbsp;
      <input name="remarks" type="text">
      <br>
      <hr>
    </td>
  </tr>
</table>
</body>

<!-- end snippet -->
英文:
  1. live is deprecated so update your jQuery library.

  2. For your question:

    1. First of all find and get selected option and add attribute selected to selected option.

    2. To get events of dynamic created elements , use event delegation.

Example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).on(&quot;change&quot;, &quot;select&quot;, function() {
  var val = $(this).val(); 
  $(&quot;option&quot;, this).removeAttr(&quot;selected&quot;).filter(function() {
    return $(this).attr(&quot;value&quot;) == val;
  }).first().attr(&quot;selected&quot;, &quot;selected&quot;);
});
$(&#39;body&#39;).on(&#39;click&#39;, &#39;input.tr_clone_add&#39;, function() {
  var $tr = $(this).closest(&#39;.tr_clone&#39;);
  var $clone = $tr.clone();
  $clone.find(&#39;.form-control js-example-basic-single&#39;).val(&#39;&#39;);
  $tr.after($clone);
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;table&gt;
  &lt;tr class=&quot;tr_clone&quot;&gt;
    &lt;td&gt;
      &lt;input type=&quot;button&quot; name=&quot;add&quot; value=&quot;clone row&quot; class=&quot;tr_clone_add&quot; /&gt;&lt;br&gt;&lt;br&gt;
      &lt;select name=&quot;animal&quot; id=&quot;number&quot;&gt;
        &lt;option value=&quot;cow&quot;&gt;cow&lt;/option&gt;
        &lt;option value=&quot;horse&quot;&gt;horse&lt;/option&gt;
        &lt;option value=&quot;dog&quot;&gt;dog&lt;/option&gt;
        &lt;option value=&quot;cat&quot;&gt;cat&lt;/option&gt;
      &lt;/select&gt;
      &amp;nbsp;
      &lt;select name=&quot;vehicle&quot; id=&quot;letter&quot;&gt;
        &lt;option value=&quot;car&quot;&gt;car&lt;/option&gt;
        &lt;option value=&quot;ship&quot;&gt;ship&lt;/option&gt;
        &lt;option value=&quot;plane&quot;&gt;plane&lt;/option&gt;
      &lt;/select&gt;
      &amp;nbsp;
      &lt;input name=&quot;remarks&quot; type=&quot;text&quot;&gt;
      &lt;br&gt;
      &lt;hr&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月9日 10:22:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定