for循环之后我的jquery代码不起作用

时间:2013-07-11 04:37:18

标签: php jquery

我正在尝试使用jQuery在表单中迭代一些textbox es。前几天工作正常,突然停止工作。我知道有些事情确实发生了错误但无法识别。如果您看到错误,请帮我解决这个问题 这是我的代码:

<?php
$serial = 0;
while ($row_details = mysql_fetch_array($res_details)) {
    $serial++;
    $remaining_qty = $row_details['ordered_qty'] - $row_details['received_qty'];
    echo '<tr>';
    echo '<td>' . $serial . '.<input type="hidden" name="pcode-' . $serial . '" id="pcode-' . $serial . '" value="' . $row_details['product_code'] . '"/></td>';
    echo '<td>' . $row_details['product_name'] . ' [' . $row_details['product_code'] . ']</td>';
    echo '<td>' . $row_details['ordered_qty'] . '</td>';
    echo '<td>' . $row_details['received_qty'] . '</td>';
    echo '<td>' . $remaining_qty . '<input type="hidden" name="remain-' . $serial . '" id="remain-' . $serial . '" value="' . $remaining_qty . '"/></td>';
    echo '<td><input type="text" name="newreceive-' . $serial . '" id="newreceive-' . $serial . '" size="5" class="money-field" value="' . $remaining_qty . '"/></td>';
    echo '</tr>';
}
?>

<script type="text/javascript">
var count = <?php echo $serial; ?>;// count is the no of rows
var verified = true;
for (var i = 1; i <= count; i++) {
    if ($("#pcode-" + i).val().length > 0) {
        if (parseInt($("#remain-" + i).val()) < parseInt($("#newreceive-" + i).val())) {
            verified = false;
        }
    }
    alert("Iteration" + i);
}

/* After this line the code does not works*/
alert("inside verify()");
if (verified) {
    alert("Verification successful");
    $("#update").removeAttr('disabled');
}
else {
    alert("Verification failed");
    $("#update").attr('disabled', 'disabled');
}
</script>

这是在函数verify()内部,在按钮单击时调用。 for循环后代码不起作用。

2 个答案:

答案 0 :(得分:0)

我认为您应该parseInt使用countloop之后break verified=false;应该使用

var count = <?php echo $serial; ?>;// count is the no of rows
var verified = true;
for (var i = 1; i <= parseInt(count); i++) {// parseInt the count
    if ($("#pcode-" + i).val().length > 0) {
        if (parseInt($("#remain-" + i).val()) < parseInt($("#newreceive-" + i).val())) {
            verified = false;
            break;// break the loop if your validation fails
        }
    }
    alert("Iteration" + i);
}

已更新尝试此操作我测试了它在我的最后工作

<script>
    $(function(){
        var count = <?php echo $serial; ?>;// count is the no of rows
        var verified = true;
        for (var i = 1; i <= parseInt(count); i++) {
            if ($("#pcode-" + i).val().length > 0) {
                if (parseInt($("#remain-" + i).val()) < parseInt($("#newreceive-" + i).val())) {
                    verified = false;break;
                }
            }
            alert("Iteration" + i);
        }

        /* After this line the code does not works*/
        alert("inside verify()");
        if (verified) {
            alert("Verification successful");
            $("#update").removeAttr('disabled');
        }
        else {
            alert("Verification failed");
            $("#update").attr('disabled', 'disabled');
        }
    });
</script>

答案 1 :(得分:0)

我发现了这个错误。实际上,当我在另一个使用count的页面中放置另一个php脚本并更新它时,我搞砸了$serial的值。这就是为什么count的值也会发生变化并导致我的jquery代码出现故障的原因。

抱歉......伙计们。这是一个愚蠢的错误。;-)