Javascript在for循环后没有发出警告

Javascript not giving alert after for loop

本文关键字:警告 for 循环 Javascript      更新时间:2023-09-26

我有一个javascript函数,其中有循环。一旦循环存在,它就不显示警报了,谁能告诉我哪里出了问题?

代码在

下面
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<?PHP Include('Includes'common'header_items.php');
session_start();
?>
</head>
<body>
 <form name="step2" method="POST">
<div id="qwe">
<table width="500px" id="myTable" name="myTable">
                            <thead>
                                <tr>    
                                    <td >Brawing / Document No.</th>
                                    <td>Revision No.</th>
                                    <td>Description     (Optional)</th>                                     
                                </tr>
                            </thead>
                            <tbody>
                            <tr>    
                                    <td width="40%"><input type="text" id="553" name="2" /></th>
                                    <td width="10%"><input type="text" id="revID553" name="3" /></th>
                                    <td width="45%"><input type="text" id="descpID553" name="4" /></th>                                     
                                </tr>
                            <tr>                                        
                                    <td width="40%"><input type="text" id="4" name="21" /></th>
                                    <td width="10%"><input type="text" id="15" name="31" /></th>
                                    <td width="45%"><input type="text" id="6" name="41" /></th>                                     
                            </tr>
                            <tr>    
                                    <td width="40%"><input type="text" id="556" name="2" /></th>
                                    <td width="10%"><input type="text" id="revID556" name="3" /></th>
                                    <td width="45%"><input type="text" id="descpID556" name="4" /></th>                                     
                            </tr>
                            </tbody>
                        </table>
<input type="submit" onclick='Javascript: return testFunc();'>
</div>
</form>
<script language="javascript">
var table_row = [];
function testFunc(){
table_row.length = 0;
var count = 0;

var testing = document.getElementById("myTable").getElementsByTagName("input");
for (i=0; i<=testing.length; i++){
var data = document.getElementById("myTable").getElementsByTagName("input")[i].id;
            if(data.substring(0,2) == "55")
            {
            var value_doc = document.getElementById("myTable").getElementsByTagName("input")[i].value;
            var value_rev = 'revID'+data;               
            var rev = document.getElementById(value_rev).value;
            var value_descp = 'descpID'+data;
            var descp_data = document.getElementById(value_descp).value;

            //code to add into array
            table_row[count] = [data,rev,descp_data];
            count ++;
            }
        }
 alert("I am in the end");
   </script>
   </body>
  </html>

我不知道为什么它不显示最后一个警报。有什么建议吗?

你好,你的代码对我来说很好。

像这样写你的函数

function testFunc(){
    alert("I am in test function");
    for (i=0; i<=5; i++){
     //code to add values in array
     // it displays the values added in array correctly
        alert('call');
    }
   alert("Function is Ending"); //this is not displayed once loop runs 5 times.
   return true;
}

现在在load中调用你的函数。

$(document).ready(function () {
     testFunc();
 });

小提琴演示

您还没有调用您的函数:

JSFiddle

检查下面的代码:

function testFunc(){
    alert("I am in test function");
    for (i=0; i<=5; i++){
     //code to add values in array
     // it displays the values added in array correctly
    }
   alert("Function is Ending"); //this is not displayed once loop runs 5 times.
   return true;
}
testFunc(); // calling function

主要问题在于额外的循环迭代。
我还重写了一些代码以避免许多document.getElementById("myTable").getElementsByTagName("input"),因为它导致每次出现时都要对DOM进行搜索。

<script language="javascript">
var table_row = [];
function testFunc() {
    table_row.length = 0;
    var count = 0;
    var testing = document.getElementById("myTable").getElementsByTagName("input");
    for (i = 0; i < testing.length; i++) {
        var data = testing[i].id;
        if (data.substring(0,2) == "55") {
            var value_doc = testing[i].value;
            var value_rev = 'revID' + data;               
            var rev = document.getElementById(value_rev).value;
            var value_descp = 'descpID' + data;
            var descp_data = document.getElementById(value_descp).value;
            //code to add into array
            table_row[count] = [data, rev, descp_data];
            count ++;
        }
    }
    alert("I am in the end");
}
</script>