简体   繁体   English

尝试从两个不同的文本输入中获取值并将它们放在同一个列表项中

[英]Trying to take Values from two different text inputs and put them in the same list item

I'm trying to take inputs from two separate fields and put them into the same line using JavaScript.我正在尝试从两个单独的字段中获取输入,并使用 JavaScript 将它们放入同一行。 So far I've been able to take the inputs but they end up on separate lines.到目前为止,我已经能够接受输入,但它们最终会出现在不同的行上。

Here is my HTML --这是我的 HTML --

   <input type="button" value="Add A Gear Item" class='btn' onclick="addGearItem()">                    </div>
                         </form>
                    </div>
                    <!---Gear List Card-->
                    <div class="card-action">
                        <h5 id='current-list'>Gear List</h5>
                        <hr>
                        <ul class='collection gear-list' id='gear-ul'>
                            <table>
                    <tr class="collection-item">
                        <td>
                           <li class="collection-item gear-item">
                            IEM Pack 1
                           </li>
                        </td>
                        <td>
                            <li class="collection-item item-cost">380</li>
                        </td>
                    </tr>
                    <tr class="collection-item gear-item">
                        <td>
                           <li class="collection-item gear-item">
                            IEM Pack 2
                           </li>
                        </td>
                        <td>
                            <li class="collection-item item-cost">380</li>
                        </td>
                    </tr>
                    </table>
                </div>
                <div class="row center">
                    <input type="submit" value="Purchase Item" class='btn'>
                </div>
            </div>

Here is my JS Function:这是我的 JS Function:

function addGearItem(){
    let setGearItem = document.getElementById('gear').value
    let setGearPrice = document.getElementById('cost').value
    const newGearItem = document.createElement('li')
    const newGearPrice = document.createElement('li')
    newGearPrice.className = "collection-item item-cost";
    newGearItem.className = "collection-item gear-item";
    newGearItem.appendChild(document.createTextNode(setGearItem));
    newGearPrice.appendChild(document.createTextNode(setGearPrice));
    document.getElementById('gear-ul').append(newGearItem);
    document.getElementById('gear-ul').append(newGearPrice);
    document.getElementById('gear').value = '';
    document.getElementById('cost').value = '';

}

I don't see a ul tag closed in your HTML file, that might be a problem.我没有在您的 HTML 文件中看到关闭的 ul 标记,这可能是个问题。 Also if you are trying to add two input values into one li element, then why are you creating two separate li tags in your JS function?此外,如果您尝试将两个输入值添加到一个 li 元素中,那么为什么要在 JS function 中创建两个单独的 li 标签?

Maybe try this:也许试试这个:

function addGearItem(){
  let setGearItem = document.getElementById('gear').value
  let setGearPrice = document.getElementById('cost').value
  const li = document.createElement('li');
  li.appendChild(document.createTextNode(`${setGearItem} ${setGearPrice}`));
  document.getElementById('gear-ul').append(li);
  document.getElementById('gear').value = '';
  document.getElementById('cost').value = '';
}

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

相关问题 如何从输入中获取多个字符串并将它们放入同一数组中? - How to take multiple strings from inputs and place them into the same array? 提取段落中的所有链接并将其放在列表中 - Take all links from a paragraph and put them in a list 如何使用 Javascript 从父 div 获取输入并从中获取实时值 - How to take the inputs from a parent div and take realtime values from them with Javascript 将表中的值拆分为不同的文本输入? - Split values from table into different text inputs? 获取变量值并将它们放在另一个文件中 - Take variable values and put them in another file 从输入文本区域获取值并将它们放在一个<div> - Take values from input text area and place them in a <div> 如何从同一个输入框中获取多个输入并存储它们? - How Do I Take Multiple Inputs From the same Input Box and store them? 尝试对不同输入的值求和/求和时,结果错误 - Wrong result while trying to sum/rest values from different inputs 我想从多个文本输入中获取输入并将其发送到我的数据库,但是只有最后一个输入发送 - I want to take input from multiple text inputs and send them to my database, but only the last input sends 每当用户更新其中之一时,如何使两个不同的范围滑块输入显示相同的值? - How to make two different range slider inputs show the same value, whenever the user updates one of them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM