2013-07-11 25 views
0

我想通过一些使用jQuery的表单迭代一些textbox es。前些天工作正常,突然停止工作。我明白某件事确实发生了错误,但无法识别。请帮我出这个毛病,如果你看到错误 这里是我的代码:我的jquery代码在for循环后不起作用

<?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循环后不起作用。

+1

你在控制台中得到任何错误?向我们展示你的'HTML'。 –

+0

感谢Gautam3164的代码格式。 –

+0

我居然在一个php脚本里面试了一下,并且放了一些alert()来查看它停止工作的地方。它在alert alert(“inside verify()”)之前工作正常;' –

回答

0

我认为你应该使用parseIntcountloop应该breakverified=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> 
+0

尝试过Rohan。没有工作。 –

+0

我想知道是什么导致代码在'for'循环之后没有执行行。我在for循环之后尝试了任何行,不起作用! –

+0

您是否在'for loop'中获得了'alert' –

0

我找到了错误。其实我搞砸了count的价值,因为我在页面中放置了另一个php脚本并且使用了$serial并进行了更新。这就是为什么count的值也改变,并导致我的jQuery代码发生故障。

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