简体   繁体   English

如何使用 select 框中的值更改输入框的类型?

[英]How can I change type of the input box using values from select box?

 $(function() { var index = 1; $("#addcon").on("click", function() { num = index + 1; $("table.contact").append(` <tr> <td class='label'><label class='required'>Contact ${num}</label></td> <td>:</td> <td><select name=contact[${index}][type] class='contact' required> <option style='display: none;' value='' selected>Select Type</option> <option>Mobile</option> <option>Landline</option> <option>Email</option> <option>Other</option> </select></td> <td><input type='text' name=contact[${index}][way] maxlength='300' class='way' required></td> </tr> `); index++; }); $("#remcon").on("click", function() { if(index - 1 >= 1) { $("table.contact tr").last().remove(); index--; } else { alert("One is must;"); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="contact"> <tr class="legend"> <td colspan="6">CONTACT DETAILS</td> </tr> <tr> <td class="label"><label class="required">Contact 1</label></td> <td>:</td> <td><select name="contact[0][type]" required class="contact"> <option style="display: none;" value="" selected>Select Type</option> <option>Mobile</option> <option>Landline</option> <option>Email</option> <option>Other</option> </select></td> <td><input type="text" name="contact[0][way]" required class="way"></td> <td style="text-align:left;"><a id="addcon" class="click">+</a></td> <td><a id="remcon" class="click">-</a></td> </tr> </table>

Above is my table in a contact detail form.以上是我的联系方式表格。 User can input any number of contacts by clicking on the "+" link and remove by clicking the "-" link.用户可以通过单击“+”链接输入任意数量的联系人,并通过单击“-”链接删除。 But in order to validate the data, I need to change the 'type' of the input field as per the selection in the respective select box (combo box).但为了验证数据,我需要根据各自 select 框(组合框)中的选择更改输入字段的“类型”。 Is there any way to do that using JQuery?有没有办法使用 JQuery 做到这一点?

Example: if I select email in select box, the type of input field respective to this select box should change to type email. Example: if I select email in select box, the type of input field respective to this select box should change to type email.

To achieve your requirement, the idea is, when creating a new row, you need to attach a change event listener to your <select> element, and when the user selects an option, the event callback will trigger an attribute change on the <input> element using jQuery's attr() method.为了实现您的要求,想法是,在创建新行时,您需要在<select>元素上附加一个更改事件侦听器,当用户选择一个选项时,事件回调将触发<input>上的属性更改<input>使用 jQuery 的attr()方法的元素。

You will also need to create some sort of a mapping between the option values and actual HTML input types that you can find here .您还需要在选项值和您可以在此处找到的实际 HTML 输入类型之间创建某种映射。

I've taken the liberty to refactor your code a little bit to create a few methods that help with code repetition.我冒昧地对您的代码进行了一些重构,以创建一些有助于代码重复的方法。 I've also move the + and - link buttons to the header of the table because there is no reason for them to be attached to the first contact.我还将+-链接按钮移至表的 header,因为没有理由将它们附加到第一个联系人。

Below is a working snippet.下面是一个工作片段。

 // mapping object from <select> values to input types const selectTypesMapping = { Mobile: 'number', Landline: 'password', Email: 'email' }; // gets a label cell const getLabelCell = (index) => (` <td class="label"> <label class="required">Contact ${index}</label> </td> `); // gets a colon cell const getColonCell = () => (`<td>:</td>`); // gets a select cell with a configured ID const getSelectCell = (index) => (` <td> <select id="select-${index}" name="contact[index][type]" class="contact" required> <option style="display: none;" value="" selected>Select Type</option> <option>Mobile</option> <option>Landline</option> <option>Email</option> <option>Other</option> </select> </td> `); // gets an input cell with a configured ID const getInputCell = (index) => (` <td> <input id="input-${index}" type="text" name="contact[index][way]" maxlength="300" class="way" required /> </td> `); // appends a row to a jQuery table and adds the change event to the select const appendRow = (jQueryTable, index) => { jQueryTable.append(` <tr> ${getLabelCell(index + 1)} ${getColonCell()} ${getSelectCell(index)} ${getInputCell(index)} </tr> `); $(`#select-${index}`).change((event) => { $(`#input-${index}`).attr('type', selectTypesMapping[event.target.value]); }); }; $(function() { // manually append 1st row appendRow($('table.contact'), 0); let index = 1; $('#addcon').on('click', function() { // append on click and increment index appendRow($('table.contact'), index++); }); $('#remcon').on('click', function() { if (index > 1) { $('table.contact tr').last().remove(); index--; } else { alert('At least one contact is required;'); } }); });
 .click { cursor: pointer; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="contact"> <tr class="legend"> <td colspan="4">CONTACT DETAILS</td> <td style="text-align:left;"><a id="addcon" class="click">+</a></td> <td><a id="remcon" title="Add" class="click">-</a></td> </tr> </table>

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

相关问题 我可以将AngularJS的dirpagination过滤器从输入框更改为选择框吗? - Can I change AngularJS's dirpagination filter from input box to select box? 如何更改输入框的文本? - How can I change the text of a input box? 如何使用 forms 使用单个按钮从输入框和下拉 select 菜单提交数据? - How can I use forms to submit data from an input box and a dropdown select menu using a single button? 如何显示日历,但不显示输入类型=日期的输入框? - How can I show calendar but not input box of input type=date? 当我 select 输入框时,如何将“您正在输入数字”放在输入框的正下方? - How can i put "you are entering a number" right below the input box when i select input box? 如何使用jquery比较两个文本输入类型框值 - how to compare two text input type box values using jquery 如何在使用jquery更改选择值时调用选择框? - how can i call select box on change while changing select value using jquery? 如何根据输入框中的值更改图像的宽度和高度? - How to change the width and height of image based on values from input box? 如何在单击时将输入框更改为选择 - how to change input box into select on clicking 如何使用reactjs在同一输入框中存储不同的值? - How can I store different values using same input box using reactjs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM