简体   繁体   English

仅使用js时如何打印选定的下拉列表选项以及文本字段?

[英]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.

相关问题 选择下拉列表时如何在其他文本字段中设置值? - How to set value in the other text field when dropdown list selected? 如何使用jQuery从多选下拉列表中打印选定的值? - how to print selected values from multiselect dropdown list using jquery? 如何编写 function 当下拉选择值将被更改并使用 js 和 html 查看输入字段中的值? - how to write a function when the dropdown selected the value will be changed and view the value in input field using js and html? 在下拉菜单中未选择任何内容时如何启用选项 - how to enable the options when nothing is selected in dropdown 选择行时用文本填充下拉字段 - Populating dropdown field with text when a row is selected 选择下拉列表时如何更改文本区域的文本 - how to change the text of a textarea when a dropdown list is selected 如何使用 JS 删除下拉列表中的选定值副本? - How to remove selected value copies in Dropdown list using JS? 如何使用Mustache.js在下拉列表中设置选定的值? - How to set a selected value in a dropdown list using Mustache.js? JS:仅从文本字段中选择数字,再加上简单的计算? - JS: Selecting Numbers Only From Text Field, Plus Simple Calculation? 使用js获取可搜索下拉列表中的选定文本 - Get selected text in searchable dropdown using js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM