简体   繁体   English

如何分离和显示选择选项数据?

[英]how to separate and display select option data?

I have two forms. 我有两种形式。 form1 's content is a choice option filled by name, matricule, and salary, and a button validate, which hides form1 and displays form2 . form1的内容是一个由名称,矩阵和薪水填充的选择选项,还有一个按钮validate,它隐藏form1并显示form2 I want when I click on the button, the contents of select to separate and show like this: 我想要当我单击按钮时, select的内容分开并显示如下:

name:
registration number :
salary :

 $(document).ready(function() { $("#hide").click(function() { let name1 = $('#name').text(); let matricule1 = $('#matricule').text(); let salary1 = $('#salary').text(); $('#nam').text(name1); $('#mat').text(matricule1); $('#sal').text(salary1); $("#form1").hide(); $("#form2").show(); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="form1"> <div class="form-group col-md-3"> <select class="form-control"> <option> <div id="name">imad</div> <div id="matricule">22</div> <div id="salary">6000</div> </option> </select> </div> <div class="form-group col-md-offset-5 "> <button class="btn btn-success " id="hide">valider</button> </div> </div> <!--form 2--> <div id="form2"> <h4>name : <span id="nam"></span></h4> <h4>matricule : <span id="mat"></span></h4> <h4>salary : <span id="sal"></span></h4> </div> 

As Rory said, you cannot have HTML within an option element, this is invalid. 正如罗里所说,您不能在option元素中包含HTML ,这是无效的。 Use a custom drop-down select library (some examples of such are here: https://www.sitepoint.com/13-jquery-selectboxdrop-down-plugins/ ) 使用自定义的下拉选择库(此处提供了一些示例: https : //www.sitepoint.com/13-jquery-selectboxdrop-down-plugins/

Now, if you know the format of the data you are using to populate the select option values with (you are getting data from server side, etc.), you could then format the data on the client side with some sort of separator to create a chained together option value for the 3 different fields, like I've done in the example below using a dash ( - ). 现在,如果您知道用来填充select option values的数据格式(您正在从服务器端获取数据,等等),则可以在客户端使用某种分隔符格式化数据以创建三个不同字段的链接在一起的option value ,就像我在下面的示例中使用破折号( - )所做的那样。 You could then split the string value of the option selected , and parse it out to individual text fields. 然后,您可以拆分option selected的字符串值,并将其解析为单个文本字段。 Again, this is all dependent on how you get and format the data within the select element. 同样,这都取决于您如何在select元素中获取和格式化数据。

 $(document).ready(function() { $("#hide").click(function() { let selectOption = $('.form-control').val(); let optionSplits = selectOption.split('-'); let name1 = optionSplits[0]; let matricule1 = optionSplits[1]; let salary1 = optionSplits[2]; $('#nam').text(name1); $('#mat').text(matricule1); $('#sal').text(salary1); $("#form1").hide(); $("#form2").show(); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="form1"> <div class="form-group col-md-3"> <select class="form-control"> <option value="imad-22-6000">imad 22 6000</option> <option value="gupta-24-8000">gupta 24 8000</option> <option value="ganesh-27-5000">ganesh 27 5000</option> </select> </div> <div class="form-group col-md-offset-5 "> <button class="btn btn-success " id="hide">valider</button> </div> </div> <!--form 2--> <div id="form2"> <h4>name : <span id="nam"></span></h4> <h4>matricule : <span id="mat"></span></h4> <h4>salary : <span id="sal"></span></h4> </div> 

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

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