我一直在尝试在Javascript文件中设置事件处理程序,但没有取得多大成功。这是我的HTML
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Order your books!</title>
<script type="text/javascript" src="scripts/Exercise2.js"></script>
</head>
<body>
<h1>Order Books Online</h1>
<fieldset>
<form>
<table>
<tr>
<td><strong>Book</strong></td>
<td><strong>Quantity</strong></td>
<td><strong>Price</strong></td>
</tr>
<tr>
<td>Basic XHTML</td>
<td><input type="text" id="XHTML"/></td>
<td>$19.99</td>
</tr>
<tr>
<td>Intro to PHP</td>
<td><input type="text" id="PHP"/></td>
<td>$86.00</td>
</tr>
<tr>
<td>Advanced JQuery</td>
<td><input type="text" id="JQuery"/></td>
<td>$55.00</td>
</tr>
</table>
<input type="submit" value="Place Order"/>
</form>
</fieldset>
</body>
这是Javascript
:
var xhtml = document.getElementById("XHTML");
var php = document.getElementById("PHP");
var jquery = document.getElementById("JQuery");
function checkFields(field) {
if (field.value === null) {
alert("Please do not leave any field empty.");
}
}
xhtml.onchange = checkFields(xhtml);
php.onchange = checkFields(php);
jquery.onchange = checkFields(jquery);
现在,即使我尝试通过更改字段的值来触发field
,控制台也会null
为onchange
。我做错了什么?
感谢。