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) {

      //你想干的事

    }
    }

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

  • 相关阅读:
    E437: terminal capability "cm" required 错误出现的原因和解决方法
    IDEA 配置svn及使用
    IDEA 2018.3.3注册方法-2100年
    三大UML建模工具Visio、Rational Rose、PowerDesign的区别
    地图展示聚合效果Leaflet.markercluster
    Idea设置Maven报错:Unable to import maven project: See logs for details
    Netflix大部分的推荐机制都是采用机器学习的算法
    Netflix推荐系统:从评分预测到消费者法则
    Netflix推荐系统的最新解读:算法、商业价值与创新
    推荐系统算法总结
  • 原文地址:https://www.cnblogs.com/menglin/p/7567906.html
Copyright © 2011-2022 走看看