为表中的输入文本框获取未定义的值? [英] Getting value as undefined for input textboxes that are in a table?

查看:19
本文介绍了为表中的输入文本框获取未定义的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含文本框的多行表格.我只想获取文本框的值并将它们添加在一起.

I have a table with multiple rows that contain textboxes. I just want to get the value of the textboxes and add them together.

我尝试过使用 innerHTML,但我得到了整个输入框的 html.如果我使用 value 我得到未定义.

I've tried using innerHTML, but I get the whole entire input box html. If I use value I get undefined.

Javascript:

Javascript:

    function getTotal()
            {
                var amt;
                var subTotal = 0;
                var FTA = document.getElementById("cphMain_gridFTA");

                for(i=1; i < FTA.rows.length ; i++)
                {

                    amt = FTA.rows[i].cells[1].value;
                    alert(amt)
                    if(isNaN(amt))
                    {
                        subTotal += 0;
                    }
                    else
                    {
                        subTotal += parseFloat(amt);
                    }


                }

            }

HTML:

    <table id="cphMain_gridFTA">
      <tbody>
        <tr>...<tr> 
        <tr>
          <td>
          ...
          </td>
          <td>
            <input name="ctl00$cphMain$gridFTA$ctl02$iTxtNumberOfCourses" type="text" value="1" id="cphMain_gridFTA_iTxtNumberOfCourses_0" onblur="getTotal()">
          </td>
        </tr>
        <tr>
          <td>
          ...
          </td>
          <td>  
            <input name="ctl00$cphMain$gridFTA$ctl03$iTxtNumberOfCourses" type="text" value="2" id="cphMain_gridFTA_iTxtNumberOfCourses_1" onblur="getTotal()">
          </td>
        </tr>
      </tbody>
    </table>

我想获取每一行的value.

推荐答案

如果你想得到 input 标签的值,那么改变你的

If you want to get the value of the input tags, then change your

document.getElementById("cphMain_gridFTA")document.getElementsByTagName("input")

这将获得 td 标签,其中包含您的 input 标签,以便您可以检索值.在您的 for 循环中,您必须将 i=1 更改为 i=0 以从 0 的第一个索引开始,这样您的 for 循环才能正确循环.在您的 for 循环中,只需获取它循环通过的每个项目的 value 并将其添加到您的 subTotal.然后我只是添加了一个警报来提醒用户 input 框的总数.希望这能帮助您找到正确的方向.

This will get the td tags which holds your input tags so you can retrieve the values. In your for loop, you must change i=1 to i=0 to start on the first index of 0 so your for loop can loop correctly. Within you for loop, just get the value of each item it loops through and add that to your subTotal. Then I just added an alert to alert the user of the total of the input boxes. Hopefully this helps get you in the right direction.

jsfiddle:https://jsfiddle.net/kyn278rq/

这篇关于为表中的输入文本框获取未定义的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆