简体   繁体   English

当所有 select 框都有一个名称时,我如何从 DOM 添加/删除 select 框,以便在提交时我从活动的 select 框中获取值?

[英]How can I add/remove select boxes from DOM when all select boxes have one name so that upon Submit I get value from active select box?

TL;DR: I want to be able to make a selection using currently-active HTML select box, when I have multiple select boxes with the same name, but only one of them is to be active at any one time. TL;DR:当我有多个 select 框时,我希望能够使用当前活动的 HTML select框进行选择,但只有一个在任何时候处于活动状态。

-- --

My question is this - I have multiple filename select boxes, that upon submit, always send the value of the last select box that is at the bottom of HTML that has filename name in HTTP Request. My question is this - I have multiple filename select boxes, that upon submit, always send the value of the last select box that is at the bottom of HTML that has filename name in HTTP Request. I want to find a way to send only the filename that is associated with the currently-active a_XX_row box, and not any others.我想找到一种方法来仅发送与当前活动的a_XX_row框相关联的filename ,而不发送任何其他文件名。

ie I select id of 40, I should only see the box that says "A-40 Designer and Specs", and not the 800 box.即我的 select id 为 40,我应该只看到“A-40 Designer and Specs”的框,而不是 800 框。 When I press submit I want POST['filename'] to have the "A-40 Designer and Specs", but instead I always get the "A-800 Designer E.xlsm", because it is the last select in the HTML file.当我按下提交时,我希望 POST['filename'] 具有“A-40 Designer and Specs”,但我总是得到“A-800 Designer E.xlsm”,因为它是 HTML 文件中的最后一个select .

 $('#modelid').on('change', function() { if (this.value == 40) $('.a_40_row').show(); else $('.a_40_row').hide(); if (this.value == 800) $('.a_800_row').show(); else $('.a_800_row').hide(); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form><table> <tr> <td> <select name="id" id="modelid"> <option value="0">--select-- <option value="40">40 <option value="800">800 </select> </td> </tr> <tr class="a_40_row" style="display: none;"> <td>Version</td> <td> <select name="filename"> <option selected>A-40 Designer and Specs B.xlsm</option> <option>A-40 Designer and Specs A.xlsm</option> </select> </td> </tr> <tr class="a_800_row" style="display: none;"> <td>Version</td> <td> <select name="filename"> <option>A-800 Designer E.xlsm</option> </select> </td> </tr> <tr> <td><input type="submit"></td> </tr> </table></form>

don't show/hide selects form.不显示/隐藏选择表单。

you just need to update the second select when the first is changed您只需要在第一个更改时更新第二个 select

the way to do that:这样做的方法:

 const myForm = document.forms['my-form'], filenames = [ { model: '40', cod: 'A-40-B', fn:'A-40 Designer and Specs B.xlsm' }, { model: '40', cod: 'A-40-A', fn:'A-40 Designer and Specs A.xlsm' }, { model: '800', cod: 'A-800-E', fn:'A-800 Designer E.xlsm' } ] myForm.modelid.onchange = () => { myForm.filename.innerHTML = '' filenames.filter(x=>x.model===myForm.modelid.value).forEach( filename=> { myForm.filename.add( new Option(filename.fn,filename.cod)) } ) }
 select, button { display: block; float: left; clear: left; margin: .3em; } select { min-width: 8em; padding: 0.3em; }
 <form name="my-form"> <select name="modelid" > <option value="" selected disable >--select--</option> <option value="40" > 40 </option> <option value="800" > 800 </option> </select> <select name="filename"></select> <button type="submit">submit</button> </form>

Two options,两种选择,

  1. Enable/disable the element as you show it.在显示元素时启用/禁用元素。
  2. Populate one field with what you need.用您需要的内容填充一个字段。

Option 1:选项1:

 $('#modelid').on('change', function() { //Set visibility $('.a_40_row').toggle(this.value == 40); //Toggle disabled $('.a_40_row [name=filename]').prop("disabled", this.value;= 40). $('.a_800_row').toggle(this.value == 800) $('.a_800_row [name=filename]'),prop("disabled". this;value;= 800); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form><table> <tr> <td> <select name="id" id="modelid"> <option value="0">--select--</option> <option value="40">40</option> <option value="800">800</option> </select> </td> </tr> <tr class="a_40_row" style="display: none;"> <td>Version</td> <td> <select name="filename" disabled> <option selected>A-40 Designer and Specs B.xlsm</option> <option>A-40 Designer and Specs A.xlsm</option> </select> </td> </tr> <tr class="a_800_row" style="display: none;"> <td>Version</td> <td> <select name="filename" disbled> <option>A-800 Designer E.xlsm</option> </select> </td> </tr> <tr> <td><input type="submit"></td> </tr> </table></form>

Option 2: - This uses HTML 5 which has issues in IE 10 and less选项 2: - 这使用 HTML 5 在 IE 10 及更低版本中存在问题

 $('#modelid').on('change', function() { $(".row").show(); var selVal = $(this).val(); //Go through the options $("[name=filename] option").each(function(){ //Dont use jquerys data here, it will convert to a number when it can //Get array of values where can display from the data attribute var arrDisplay = this.dataset.display.split(","); //Add the hidden property if not contained in array -- wont work in IE 10 and older $(this).prop("hidden", .arrDisplay;includes(selVal)). //Set a default $(this),prop("selected". $(this).data("optiondefault") &&;arrDisplay;includes(selVal) ) }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form><table> <tr> <td> <select name="id" id="modelid"> <option value="0">--select--</option> <option value="40">40</option> <option value="800">800</option> </select> </td> </tr> <tr class="row" style="display: none;"> <td>Version</td> <td> <select name="filename"> <option data-display="40" data-optiondefault="true">A-40 Designer and Specs B.xlsm</option> <option data-display="40">A-40 Designer and Specs A.xlsm</option> <option data-display="800" data-optiondefault="true">A-800 Designer E.xlsm</option> <option data-display="40,800">Hybrid</option> </select> </td> </tr> <tr> <td><input type="submit"></td> </tr> </table></form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM