简体   繁体   English

如何在另一个Java脚本功能内的Java脚本中访问动态html表中单元格的动态分配值

[英]How to access the dynamically assigned value for a cell in the dynamic html table in java script within another java script function

I have a problem with accessing the cell value that I have assigned to a cell in the dynamic table using a addRow js function.Now I want to access that value from another js function function re(cal) on button click but when I try to access that value it says that value/innerHtml is undefined. 我在使用addRow js函数访问分配给动态表中单元格的单元格值时遇到问题。现在我想在单击按钮时从另一个js函数function re(cal)访问该值,但是当我尝试访问该值时,它表示value / innerHtml是未定义的。

Here is my html file 这是我的html文件

        <table  id="calorie">
            <tr><hr></tr>   

        <tr>
        <td><label class="z"><b>Food Item</b></label></td>
    <td> <?php 

                include("connection.php");
                $query = "SELECT * FROM  foodcataloge";
                $result = mysql_query($query) or die("Unable to query Food");
                $option = '';
                    while($row=mysql_fetch_array($result)) {
                        $option .= '<option value='.$row['FoodCataloge_CalorieAmount'].'>'. $row['FoodCataloge_FoodName'].'</option>';
        }
?>
    <select name="FoodCataloge_FoodName" id="food" onchange="updateText();"><?php echo $option; ?></select></td>
        </tr>

        <tr>
        <td><label class="z"><b>Portion </b></label></td>
        <td> <input type="text"class="span3" name="foodpor" id="foodpor" onchange="pupdate();"></td>
        </tr>

        <tr>
        <td><label class="z"><b>Calorie amount per portion </b></label></td>
        <td> <input type="text"class="span3" name="calamount" id="calamount"  ></td>
        </tr>

        <tr><td><label class="z"><b>Number of Calories</b></label></td>
        <td> <input type="text" class="span3"name="calo" id="calo"></td>
        </tr>
        <tr>

        <tr><td><label class="z"><b>Total</b></label></td>
        <td> <input type="text" class="span3"name="res" id="res"></td>
        </tr>
        <tr>
        <td></td><td></td>


            <td><input type = "button" value = "Add" onclick = "showDiv(),addRow('cal')"></td>
            <td><input type = "button" value = "Remove" onclick = "deleteRow('cal')"></td>
            <td><input type = "button" value = "Calculate" onclick = "re('cal')"></td></tr>


        </table>


            <br><br><br><br>

        <table id="cal" class="table span2"  >

        <thead>
        <th width="30px" id="l1" hidden="true"><label class="z"><b>Remove</b></label></th>
        <th width="30px" id="l2"hidden="true"><label class="z"><b>Food</b></label></th>
        <th width="30px" id="l3" hidden="true"><label class="z"><b>Portion</b></label></th>
        <th width="30px" id="l4" hidden="true"><label class="z"><b>Calories</b></label></th>
        <th width="30px" id="l5" hidden="true"><label class="z"><b> Total</b></label></th>


        </thead>



        </table>

my dynamic table is cal 我的动态表是cal

Here is my js file 这是我的js文件

function re(cal){


            var table = document.getElementById(cal);

           var rowCount = table.rows.length;
alert(rowCount);

    //or (var i=1;i<rowCount;i++){
        var sum =document.getElementById(cal).rows[1].cells[3].value;
        alert(sum);
//}

}

function addRow(cal) {
         document.getElementById('cal').style.border = "8px solid #718294";
            var table = document.getElementById(cal);

            var rowCount = table.rows.length;

            var cellCount= document.getElementById(cal).rows[0].cells.length;


            var row = table.insertRow(rowCount);
            row.id="row"
            var cell1 = row.insertCell(0);
            var element1 = document.createElement("input");
            element1.type = "checkbox";
            element1.name="chkbox[]";
            cell1.appendChild(element1);

            var cell2= row.insertCell(1);
                var element2 = document.createElement("input");
                element2.type = "text";
                element2.name = "txtbox[]";
                element2.value=document.getElementById('food').options[document.getElementById('food').selectedIndex].text;
                element2.setAttribute('id', 'cell2' + rowCount);
                cell2.appendChild(element2);

            var cell3= row.insertCell(2);
                var element3 = document.createElement("input");
                element3.type = "text";
                element3.name = "txtbox[]";
                element3.value=document.getElementById('foodpor').value;
                element3.setAttribute('id', 'cell3' + rowCount);
                cell3.appendChild(element3);

                var cell4= row.insertCell(3);
                var element4 = document.createElement("input");
                element4.type = "text";
                element4.name = "txtbox[]";
                element4.value=document.getElementById('calo').value;
                element4.setAttribute('class', 'clu');
                element4.setAttribute('id', 'cell4' + rowCount);
                cell4.appendChild(element4);

                var cell5= row.insertCell(4);
                var element5 = document.createElement("input");
                element5.type = "text";
                element5.name = "txtbox[]";
                element5.setAttribute('id', 'cell5' + rowCount);
                cell5.appendChild(element5);



            //cell2.innerHTML=document.getElementById('food').options[document.getElementById('food').selectedIndex].text;
            //cell3.innerHTML=document.getElementById('foodpor').value;
            //cell4.innerHTML=document.getElementById('calo').value;




            document.getElementById('food').value = '';
            document.getElementById('foodpor').value='';
            document.getElementById('calo').value='';
            document.getElementById('calamount').value='';
        }

        function deleteRow(cal) {
            try {
            var table = document.getElementById(cal);
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[0];
                if(null != chkbox && true == chkbox.checked) {
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }


            }
            }catch(e) {
                alert(e);
            }
        }

All the other things working fine but from the function function re(cal) alert the sum as undefined but I expect it to be the value of that selected cell.. 其他所有功能都function re(cal)运行,但function re(cal)警告sum未定义,但我希望它是该选定单元格的值。

Please tell me what I am doing wrong! 请告诉我我做错了!

From where are you calling re(cal)? 您从哪里叫re(cal)? the alert? 警报? before or after the table creation. 在创建表之前或之后。 If before, that's why it's undefined. 如果以前,那就是为什么它没有定义的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在同一个 function java 脚本中访问另一个 scope 中的数组? - how access to array in another scope in same function java script? 如何在Java脚本中选择分配给Django变量的html id - how to select html id which is assigned to Django variable in java script 如果 HTML 选择器具有特定值,如何运行 Java 脚本 function? - How to run a Java Script function if an HTML selector has a specific value? 如何为通过Java脚本动态创建的HTML表行放置onclick事件? - How to put an onclick event for a HTML table row created dynamically through java script.? 在java脚本中拖动一个html表列值并将列值增加1 - Dragging a html table column value and increment the column value by 1 in java script 如何使用Java脚本在html动态表中显示编辑和删除按钮 - how to Show edit and delete buttons in html dynamic table using java script 如何动态删除选定的下拉值用户可以使用 Java 脚本选择或取消选择 HTML? - How to remove selected drop down value in dynamically user can select or unselect HTML using Java script? 如何使用java脚本更改表中单元格的背景颜色 - How to change background color of cell in table using java script 动态访问java脚本中的表单元素 - access form elements in java script dynamically 如何从 java 脚本文件中调用 function 在另一个 function 内? - How do I call a function inside of another function from java script file in html file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM