我找不到爲什麼我的函數沒有被調用。我把alert("test")
放在那裏只是爲了測試函數是否被調用,而不是。名稱爲「clear」的函數未被調用。試圖用內聯js呼叫「clear()」
HTML:
<input type="button" onclick="clear(price,quantity)" value="Clear">
JS:
function clear(price,quantity){
alert("test");
i=0;
if(i<5){
price[i].value = "";
quantity[i].value = "";
i++;
}
}
編輯:這裏的所有代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Amount Owed</title>
<script type = "text/javascript">
function amount(price, quantity){
sum1 = price[0].value * quantity[0].value;
sum2 = price[1].value * quantity[1].value;
sum3 = price[2].value * quantity[2].value;
sum4 = price[3].value * quantity[3].value;
return sum1 + sum2 + sum3 + sum4;
}
function clear(price,quantity){
alert("test");
i=0;
if(i<5){
price[0].value = "";
quantity[0].value = "";
i++;
}
}
</script>
</head>
<body>
<h1>Amount Owed</h1>
<p>Enter price for first amount and quantity for each item</p>
<form>
<table>
<tr>
<td>PRICE</td>
<td>QUANTITY</td>
</tr>
<tr>
<td><input type="text" name="price" size="6"></td>
<td><input type="text" name="quantity" size="6"></td>
</tr>
<tr>
<td><input type="text" name="price" size="6"></td>
<td><input type="text" name="quantity" size="6"></td>
</tr>
<tr>
<td><input type="text" name="price" size="6"></td>
<td><input type="text" name="quantity" size="6"></td>
</tr>
<tr>
<td><input type="text" name="price" size="6"></td>
<td><input type="text" name="quantity" size="6"></td>
</tr>
</table>
<input type="button" onclick="pay.value = amount(price,quantity)" value="Total">
<input type="button" onclick="clear(price,quantity)" value="Clear">
<p>The total is: <input type="text" id="pay" name="pay"></p>
</form>
</body>
</html>
您的JavaScript控制檯中是否有任何錯誤? –
你在哪裏宣佈js? –
@PedroL。 - 什麼時候沒關係。直到click事件觸發並且函數不嘗試觸摸DOM時纔會調用該函數。 – Quentin