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