可能重复:
如何调试 Javascript 错误?
我创建了一个带有 HTML 表单的表格,如下所示: // $usr 只是另一个过程的结果:
<form name="myForm" action="addAccessories.php" method="post" onsubmit="return validateForm(this);">
<table border="1" id="IT">
<tr>
<th>Barcode<em>*</em></th>
<th>Current stock</th>
</tr>
<?php
for($id=1; $id<=$usr; $id++){
?>
<tr>
<td><input type="hidden" name="id[]" value="<?php echo $id; ?>" />
<input type="text" name="bar_code<?php echo $id; ?>" id="bar_code<?php echo $id; ?>" value="" /></td>
" type="text" id="description" value="" />
<td><input type="text" name="num_stock<?php echo $id; ?>" value="0" id="num_stock<?php echo $id; ?>"/></td>
<?php
}
?>
<tr>
<td> </td>
<td> <button data-theme="b" input type="submit" name="Submit" value="Submit">Add accessories</button></td>
</tr>
</table>
</form>
获取输入并创建表后,我使用验证表单代码对其进行验证(验证和自动完成代码):
<?php
$query= "SELECT description from accessories_type ORDER BY description";
$result= mysqli_query($link, $query) or die(mysqli_error($link));
?>
<script>
$(document).ready(function() {
for(i = 1; i < document.getElementById("IT").rows.length; i++) {
var availableConsoles = [
<?php
while($row = mysqli_fetch_array($result)){
echo '"' . $row['description'] . '"' . ',';
}
?>
];
$( "#description " ).autocomplete({
source: availableConsoles
});
}
});
</script>
<script type="text/javascript">
function validateForm(form){
var errors = [];
var c, cs = form.elements;
var reB = /^bar_code/;
var reN = /^num_stock/;
var reD = /^[0-9]+$/;
for (var i=0;i<cs.length; i++) {
c = cs[i];
if (reB.test(c.name) && c.value == '') {
errors[errors.length]= 'You must enter barcode.';
}
if (reN.test(c.name) && !reD.test(c.value)) {
errors[errors.length] = 'Stock number must be a positive integer.';
}
if (errors.length > 0) {
reportErrors(errors);
return false;
} return true;
}
}
function reportErrors(errors){
var msg = "There were some problems...\n";
for (var i = 0; i<errors.length; i++) {
var numError = i + 1;
msg += "\n" + numError + ". " + errors[i];
}
alert(msg);
}
</script>
但是,它并没有按我的意愿执行。谁能指出我做错了什么以及如何解决?另外,如何调试一般的 javascripts 代码(例如将代码执行结果打印到终端,类似于“echo .... in PHP”)谢谢您的帮助。