[英]how to print selected dropdown list options plus text field when using js only?
Good afternoon Stackoverflow folks, 下午好,Stackoverflow的人们,
I have the following javascript code, and I want to pass the dorpdown option + text filed into one singe string: Example "Mr. John" "Mrs. Johna" The number of members has to be defined before filling out. 我有以下javascript代码,并且我想将dorpdown选项+文本传递到一个单一字符串中:示例“ John先生”“ Johna先生”在填写之前必须定义成员数。
Is there a way of doing this with html? 有没有办法做到这一点的HTML? (this is one field of an html form) or creating a second function showFields()
(这是html表单的一个字段)或创建第二个函数showFields()
I am trying with jQuery but till now I havent succeeded. 我正在尝试使用jQuery,但直到现在我还没有成功。
function addFields() {
var number = document.getElementById('member').value;
var container = document.getElementById('container');
var optionsSelect = [
{
id: 1,
title: 'Mr'
},
{
id: 2,
title: 'Mrs'
}
];
while (container.hasChildNodes()) {
container.removeChild(container.lastChild);
}
for (let i = 0; i < number; i++) {
var select = document.createElement('select');
for (var j = 0; j < optionsSelect.length; j++) {
var options = document.createElement('option');
options.value = optionsSelect[j].id;
options.text = optionsSelect[j].title;
select.appendChild(options);
}
container.appendChild(select);
container.appendChild(document.createTextNode(' -> Name ' + (i + 1)));
var input = document.createElement('input');
input.type = 'text';
container.appendChild(input);
container.appendChild(document.createElement('br'));
}
}
<input type="text" id="member" name="member" value="">Number of members: (max. 10)<br />
<a href="#" id="filldetails" onclick="addFields()">Fill Details</a>
<div id="container"/>
Code can be tested here 可以在这里测试代码
Any tips are welcome! 欢迎任何提示!
What you're looking for is reduce how to take a data set and 'reduce' it into one thing. 您正在寻找的是减少如何获取数据集并将其“简化”为一件事。
another thing to look into is template literals to make things cleaner makes constructing strings very easy. 要研究的另一件事是模板文字,使内容更整洁,使构造字符串非常容易。
I've also done it so it maps to an array and i'd suggest going down this route. 我也这样做了,所以它映射到一个数组,我建议沿着这条路线走。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Template_literals
const optionsSelect = [{ id: 1, title: 'Mr' }, { id: 2, title: 'Mrs' } ]; function getResults() { const { selects, inputs } = getInputs(); return selects.reduce((acc, select, i) => { const { title, name } = getValuesFromElements(select, inputs[i]); return (title && name) ? `${acc} ${title}. ${name}` : acc; }, ''); } function getResultsAsArray() { const { selects, inputs } = getInputs(); const getFieldValues = (select, i) => { const { title, name } = getValuesFromElements(select, inputs[i]); return (title && name) ? `${title}. ${name}` : ''; }; return selects.map(getFieldValues).filter(Boolean); } function getValuesFromElements(select, {value: name}) { const { title } = optionsSelect[select.value - 1]; return {title, name}; } function getContainerElements(query) { return Array.from(document.querySelectorAll(`#container > ${query}`)); } function getInputs() { const selects = getContainerElements('select'); const inputs = getContainerElements('input'); return { selects, inputs } } function addFields() { const { value: number } = document.getElementById('member'); const container = document.getElementById('container'); while (container.hasChildNodes()) { container.removeChild(container.lastChild); } for (let i = 0; i < number; i++) { const select = document.createElement('select'); for (let j = 0; j < optionsSelect.length; j++) { const options = document.createElement('option'); options.value = optionsSelect[j].id; options.text = optionsSelect[j].title; select.appendChild(options); } container.appendChild(select); container.appendChild(document.createTextNode(' -> Name ' + (i + 1))); const input = document.createElement('input'); input.type = 'text'; container.appendChild(input); container.appendChild(document.createElement('br')); } }
<input type="text" id="member" name="member" value="">Number of members: (max. 10)<br /> <a href="#" id="filldetails" onclick="addFields()">Fill Details</a> <a href="#" onclick="console.log(getResults())">Log results</a> <a href="#" onclick="console.log(getResultsAsArray())">Log results as array</a> <div id="container"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.