尝试在每次单击时从对象数组生成新的随机SKU,但始终收到ReferenceError
。
这是我的方法:
// example of the data
[{sku: '101ZS536',
price: 4.37
},
{
sku: '10134QYT',
price: 42.61
},
{
sku: '10134QYT',
price: 53.31
},
{
sku: '10134QYT',
price: 0
},
{
sku: '10134QYT',
price: 3.46
}
]
//index.html
<div class="container">
<input type="button" id="SKUsearch" value="Search" onclick="randomSKU()" />
<p id="message" ></p>
</div>
//index.js
function bootRentData(bookData) {
bookData.forEach((book) => {
//generate random sku number
const sku = book.sku
function randomSKU(){
var random = sku[Math.floor(Math.random() * sku.length)];
document.getElementById("message").innerHTML = sku[random];
}
});
wrap.innerHTML += `<tr>
<td>${book.sku}</td>
<td>${book.price}</td>
</tr>`
}
使我遇到以下错误:
Uncaught ReferenceError: randomSKU is not defined
at HTMLInputElement.onclick ((index):15)