js计数在html元素中显示为Nan

wswtfjt7  于 2022-12-16  发布在  其他
关注(0)|答案(1)|浏览(239)

我遇到了一个问题,在我的js的这一部分中的任何+ count +,都返回为第一个之外的Nan,也就是这个<th scope="row" class="product-id">' + count + '</th>

var count = 1;

function new_link() {
  count++;
  var e = document.createElement("tr");
  e.id = count, e.className = "product";
  var t = '<tr><th scope="row" class="product-id">' + count + '</th><td class="text-start"><div class="mb-2"><input class="form-control bg-light border-0" type="text" name="productName-' + count + '" id="productName-' + count + '" placeholder="Product Name"></div><textarea class="form-control bg-light border-0" id="productDetails-' + count + '" rows="2" placeholder="Product Details"></textarea></div></td><td><input class="form-control bg-light border-0 product-price" type="number" id="productRate-' + count + '" step="0.01" placeholder="$0.00"></td><td><div class="input-step"><button type="button" class="minus">–</button><input type="number" class="product-quantity" id="product-qty-' + count + '" value="0" readonly><button type="button" class="plus">+</button></div></td><td class="text-end"><div><input type="text" class="form-control bg-light border-0 product-line-price" id="productPrice-' + count + '" value="$0.00" placeholder="$0.00" /></div></td><td class="product-removal"><a class="btn btn-success">Delete</a></td></tr>';
}

我需要所有的+ counts +给予一个数值,以便我可以$_POST他们以后。大多数是在html元素。我不知道从这里去。

dgtucam1

dgtucam11#

这就是我的解决方案!
我还将var改为const,将撇号(')改为反勾号(')!

let count = 1;

function new_link() {
    count++;
    const e = document.createElement("tr");
    e.id = count;
    e.className = "product";
    const t = `
        <th scope="row" class="product-id">${count}</th>
        <td class="text-start">
            <div class="mb-2">
                <input class="form-control bg-light border-0" type="text" name="productName-${count}" id="productName-${count}" placeholder="Product Name" />
            </div>
            <textarea class="form-control bg-light border-0" id="productDetails-${count}" rows="2" placeholder="Product Details"></textarea>
            </div>
        </td>
        <td>
            <input class="form-control bg-light border-0 product-price" type="number" id="productRate-${count}" step="0.01" placeholder="$0.00">
        </td>
        <td>
            <div class="input-step">
                <button type="button" class="minus">-</button>
                <input type="number" class="product-quantity" id="product-qty-${count}" value="0" readonly />
                <button type="button" class="plus">+</button>
            </div>
        </td>
        <td class="text-end">
        <div>
            <input type="text" class="form-control bg-light border-0 product-line-price" id="productPrice-${count}" value="$0.00" placeholder="$0.00" />
        </div>
        </td>
        <td class="product-removal">
            <a class="btn btn-success">Delete</a>
        </td>
    `;
    e.innerHTML = t;
    return e;
}

console.log(new_link());
console.log(new_link());
console.log(new_link());

相关问题