doulvyi2172 2012-06-10 15:06
浏览 26

如何在Javascript中调试验证表单? [重复]

Possible Duplicate:
How to debug Javascript error?

I have created a table with HTML form as follow: // $usr is just a result of another process:

<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>   

After the inputs are taken and table is created I used the validation form codes to validate it (validation and auto-completion codes):

<?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...
";
for (var i = 0; i<errors.length; i++) {
    var numError = i + 1;
    msg += "
" + numError + ". " + errors[i];
}
alert(msg);
}
</script> 

However, it does not perform as I wanted. Can anyone point out what I did wrong and how to fix it? Also, how to debug javascripts codes in general (like print out to terminal the result of codes execution similar to "echo .... in PHP") Thank you for your help.

  • 写回答

1条回答 默认 最新

  • drk49438 2012-06-10 15:21
    关注

    i dont know your problem but here's some things to do:

    instead of:

    errors[errors.length] = "Current stock must be an integer";
    

    do:

    errors.push("Current stock must be an integer");
    

    at the end you can call console.log as @tereško said and press F12 to check it out:

    console.log(errors);
    

    also this:

    if(isNaN(num_stock)){
    

    could be this (for safety):

    if(isNaN(parseInt(num_stock))){
    

    now, what errors that errors array produces?

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度