0

我正在尝试textbox使用 jQuery 遍历表单中的一些 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循环后代码不起作用。

4

2 回答 2

0

我认为你应该使用parseIntforcountloopshould breakafter verified=false;like,

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>
于 2013-07-11T05:06:40.450 回答
0

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

对不起……伙计们。这是一个愚蠢的错误。;-)

于 2013-07-11T05:35:41.960 回答