zoukankan      html  css  js  c++  java
  • JS获取table input输入框的值

    var lineHtml = "";
    lineHtml += ' <tr>';
    lineHtml += ' <td id="RowNo">' + gIndex + '</td>';
    lineHtml += ' <td ><input type="checkbox" name="chk" id="chk' + gIndex + '" onkeyup="onkeydownHandler(event,this,' + gIndex + ');" value="1" /></td>';
    lineHtml += ' <td style="display:none" id="ProductID">' + gJson[i].ProductID + '</td>';
    lineHtml += ' <td id="ProductNo">' + gJson[i].ProductNo + '</td>';
    lineHtml += ' <td id="Qty">' + gJson[i].Qty + '</td>';
    lineHtml += ' <td id="InQty"><input type="text" name="InQty" id="InQty' + gIndex + '" class="inputCell txtInput" onkeyup="onkeydownHandler(event,this,' + gIndex + ');" oninput="calculate(this,' + gIndex + ');" value="' + gJson[i].InQty + '" /></td>';

    lineHtml += ' <td id="PriceTax"><input type="text" name="PriceTax" id="PriceTax' + gIndex + '" class="inputCell txtInput" onkeyup="onkeydownHandler(event,this,' + gIndex + ');" oninput="calculate(this,' + gIndex + ');" value="' + gJson[i].PriceTax + '" /></td>';
    lineHtml += ' <td id="ItemSum" ><input type="text" name="ItemSum" id="ItemSum' + gIndex + '" class="inputCell txtInput" onkeyup="onkeydownHandler(event,this,' + gIndex + ');" oninput="calculate(this,' + gIndex + ');" value="' + gJson[i].ItemSum + '" /></td>';
    lineHtml += ' <td id="BatchNo"><input type="text" name="BatchNo" id="BatchNo' + gIndex + '" class="inputCell txtInput" onkeyup="onkeydownHandler(event,this,' + gIndex + ');" oninput="calculate(this,' + gIndex + ');" value="' + gJson[i].BatchNo + '" /></td>';
     lineHtml += '    </tr>';

    如果我们用js组合了这样的一个table,想要获取其中的值,怎么获取呢?
    我们可以看到里边有两种组合方式:

    1.方式一: 直接绑定型

    lineHtml += ' <td id="ProductNo">' + gJson[i].ProductNo + '</td>';

    此时要获取ProductNo的值:

     var tableObj = document.getElementById('tb');

     var ProductNo = tableObj.rows[i + 1].cells["ProductNo"].innerText;

    2.方式二:控件输入型

    ******* type=“text”********

    lineHtml += ' <td id="BatchNo"><input type="text" name="BatchNo" id="BatchNo' + gIndex + '" class="inputCell txtInput" onkeyup="onkeydownHandler(event,this,' + gIndex + ');" oninput="calculate(this,' + gIndex + ');" value="' + gJson[i].BatchNo + '" /></td>';

    此时要获取我们输入的BatchNo的值: 

    var BatchNo = tableObj.rows[i + 1].cells.BatchNo.childNodes["0"].value;

    ********type=“checkbox”*******

    lineHtml += ' <td ><input type="checkbox" name="chk" id="chk' + gIndex + '" onkeyup="onkeydownHandler(event,this,' + gIndex + ');" value="1" /></td>';

    此时我们要获取checkbox是否被选中

     var chkList = $("[name='chk']");

    for (var i = 0; i < chkList.length; i++) {
    var isChecked = chkList[i].checked;
    if (isChecked == true) {

      //你想干的事

    }
    }

    ******本文原创,禁止转载************

  • 相关阅读:
    JS实现继承的几种方式
    跨平台APP----对Cordova,APPCan,DCloud,APICloud四大平台的分析
    cordova生成的android项目导入到Android studio 2.X 中遇到的问题解决方案
    链操作相关命令(包括启动,重启,删除)
    冷钱包和热钱包有什么区别?
    常用命令之git/linux
    centos安装git,go,shasum,okexchain环境
    iterm2的下载安装与配置
    使用jsdoc-to-markdown提前js文件的文档
    基于sphinx的文档(一)将md转为rst
  • 原文地址:https://www.cnblogs.com/menglin/p/7567906.html
Copyright © 2011-2022 走看看