保留一个选定选项的克隆下拉菜单值并重置第二个下拉菜单 jQuery

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

After clone dropdown keep value of one selected option and reset the second dropdown jquery

问题

  1. 如何在克隆新行时保留区域下拉菜单的选定值,并使服务下拉菜单的结果保持未选定状态?

  2. 我尝试使用select2,但它无法应用于所有下拉菜单,尽管我尝试销毁它们,例如:$("select").select2("destroy");

英文:

I have a table with 2 linked dropdowns that I must clone on select the Services dropdown. The second dropdown is popped up according to the selection of the first dropdown (this part works).
I successfully clone them. However, each clone must keep the selected value of the Area dropdown and the Services dropdown must keep the result but with unselected option.

 		<table class="table table-sm table-striped mb-0">
	<thead>
	<tr class="bg-th">
	<th >
	Country
	</th>
	<th scope="col">
	Towns
	</th>

	</tr>
	</thead>

	<tbody>
	<tr>
	<td >
	<select class="form-select" onChange="center_area_on_change(this);">
	<option value=""></option>

	</select>
	</td>
	<td style="width:280px;display:block">

	<select style='width:100%' onChange="getTarifDataSeguroPrivado(this);">


	</select>


	</td>

	</tr>
  </tbody>
	</table>

*The center_area_on_change function pops up the Services dropdown on select.

*The getTarifDataSeguroPrivado function calls the function that clones the row (function addRow).

function getTarifDataSeguroPrivado(dropDown) {

  addRow();

}

保留一个选定选项的克隆下拉菜单值并重置第二个下拉菜单 jQuery
The addRow() function.*

function addRow() {

var tableBody = $('#table-tarifario').find("tbody");
var trLast = tableBody.find("tr:last");

var trNew = trLast.clone();


trNew.find('select').val(function(index, value) {
return trLast.find('select').eq(index).val();
});

trLast.after(trNew);

}
  1. How can I keep the selected value of the area dropdown, and keep the result of the services dropdown with not selected data when a new row is cloned?

2- I tried to use select2, but it cannot apply to all the dropdowns, although I destroy them like:
$("select").select2("destroy");

答案1

得分: 1

以下是您要翻译的代码部分:

I'm not sure that I fully understand what you are asking. Also, I found it difficult to reproduce your issue because you provided code seems incomplete - the `<select>` menus have no options and the table selector, `'#table-tarifario'`, does not exist.

However, I am providing some code that should at least get you close to what you are looking for. I am taking the index of the selected option in the last row and using it to set the selected option on the new row.

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

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

    function center_area_on_change (el) {
      console.log(el);
    }

    function getTarifDataSeguroPrivado(dropDown) {
      addRow();
    }

    function addRow() {
      $('table select').select2('destroy');
      var tableBody = $('table').find("tbody");
      var trLast = tableBody.find("tr:last");
      var trNew = trLast.clone();

      const lastSelectedCountryIndex = trLast.find('select').eq(0).find('option:selected').index();

      trNew.find('select').eq(0).find('option').eq(lastSelectedCountryIndex).prop('selected', true);

      trLast.after(trNew);
      $('table select').select2();
    }

    $('table select').select2();


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

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet"/>
     <table class="table table-sm table-striped mb-0">
       <thead>
         <tr class="bg-th">
           <th>
             Country
           </th>
           <th scope="col">
             Towns
           </th>

         </tr>
       </thead>

       <tbody>
         <tr>
           <td>
             <select class="form-select" onChange="center_area_on_change(this);">
                <option value=""></option>
                <option>Country 1</option>
                <option>Country 2</option>
                <option>Country 3</option>
                <option>Country 4</option>
             </select>
           </td>
           <td style="width:280px;display:block">

             <select style='width:100%' onChange="getTarifDataSeguroPrivado(this);">
              <option></option>
              <option>Town 1</option>
              <option>Town 2</option>
              <option>Town 3</option>
              <option>Town 4</option>
              <option>Town 5</option>
             </select>


           </td>

         </tr>
       </tbody>
     </table>

<!-- end snippet -->

Alternatively, if you have `value` attributes on your options, you could do this by value:

function addRow() {
var tableBody = $('table').find("tbody");
var trLast = tableBody.find("tr:last");
var trNew = trLast.clone();

const lastSelectedCountryIndex = trLast.find('select').eq(0).find('option:selected').index();

const lastSelectedCountry = trLast.find('select').eq(0).val();

trNew.find('select').eq(0).val(lastSelectedCountry);

trLast after(trNew);
}


Update: When using [Select2][1], you must be sure to remove Select2 - `$('select').select2('destroy')` - before cloning. Once the cloned row has been inserted into the DOM, you can re-apply Select2 - `$('select').select2()`. I have updated the code example to use Select2.
[1]: https://select2.org/
英文:

I'm not sure that I fully understand what you are asking. Also, I found it difficult to reproduce your issue because you provided code seems incomplete - the &lt;select&gt; menus have no options and the table selector, &#39;#table-tarifario&#39;, does not exist.

However, I am providing some code that should at least get you close to what you are looking for. I am taking the index of the selected option in the last row and using it to set the selected option on the new row.

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

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

function center_area_on_change (el) {
console.log(el);
}
function getTarifDataSeguroPrivado(dropDown) {
addRow();
}
function addRow() {
$(&#39;table select&#39;).select2(&#39;destroy&#39;);
var tableBody = $(&#39;table&#39;).find(&quot;tbody&quot;);
var trLast = tableBody.find(&quot;tr:last&quot;);
var trNew = trLast.clone();
const lastSelectedCountryIndex = trLast.find(&#39;select&#39;).eq(0).find(&#39;option:selected&#39;).index();
trNew.find(&#39;select&#39;).eq(0).find(&#39;option&#39;).eq(lastSelectedCountryIndex).prop(&#39;selected&#39;, true);
trLast.after(trNew);
$(&#39;table select&#39;).select2();
}
$(&#39;table select&#39;).select2();

<!-- 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;script src=&quot;https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js&quot;&gt;&lt;/script&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css&quot; rel=&quot;stylesheet&quot;/&gt;
&lt;table class=&quot;table table-sm table-striped mb-0&quot;&gt;
&lt;thead&gt;
&lt;tr class=&quot;bg-th&quot;&gt;
&lt;th&gt;
Country
&lt;/th&gt;
&lt;th scope=&quot;col&quot;&gt;
Towns
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;select class=&quot;form-select&quot; onChange=&quot;center_area_on_change(this);&quot;&gt;
&lt;option value=&quot;&quot;&gt;&lt;/option&gt;
&lt;option&gt;Country 1&lt;/option&gt;
&lt;option&gt;Country 2&lt;/option&gt;
&lt;option&gt;Country 3&lt;/option&gt;
&lt;option&gt;Country 4&lt;/option&gt;
&lt;/select&gt;
&lt;/td&gt;
&lt;td style=&quot;width:280px;display:block&quot;&gt;
&lt;select style=&#39;width:100%&#39; onChange=&quot;getTarifDataSeguroPrivado(this);&quot;&gt;
&lt;option&gt;&lt;/option&gt;
&lt;option&gt;Town 1&lt;/option&gt;
&lt;option&gt;Town 2&lt;/option&gt;
&lt;option&gt;Town 3&lt;/option&gt;
&lt;option&gt;Town 4&lt;/option&gt;
&lt;option&gt;Town 5&lt;/option&gt;
&lt;/select&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

Alternatively, if you have value attributes on your options, you could do this by value:

function addRow() {
var tableBody = $(&#39;table&#39;).find(&quot;tbody&quot;);
var trLast = tableBody.find(&quot;tr:last&quot;);
var trNew = trLast.clone();
const lastSelectedCountryInddex = trLast.find(&#39;select&#39;).eq(0).find(&#39;option:selected&#39;).index();
const lastSelectedCountry = trLast.find(&#39;select&#39;).eq(0).val();
trNew.find(&#39;select&#39;).eq(0).val(lastSelectedCountry);
trLast.after(trNew);
}

Update: When using Select2, you must be sure to remove Select2 - $(&#39;select&#39;).select2(&#39;destroy&#39;) - before cloning. Once the cloned row has been inserted into the DOM, you can re-apply Select2 - $(&#39;select&#39;).select2(). I have updated the code example to use Select2.

huangapple
  • 本文由 发表于 2023年4月20日 08:10:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059660.html
匿名

发表评论

匿名网友

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

确定