douzhenzu0247
2017-10-13 04:53
浏览 201
已采纳

jquery - 将data-rowindex值动态存储到数组中

I want to save data-rowindex value dynamically into array

<tr class="ewTableRow" data-rowindex="1" id="r1_assessment_training" data-rowtype="2">

I tried below code. (I am not sure weather below code is correct way or not)?

var a = $('#tbl_assessment_traininggrid tr').length;

var row_count = [];
    for (var i = 1; i <= a; i++) {
        var rowCntrl = $("data-rowindex[i]").val();
        alert(rowCntrl);
        row_count.push(rowCntrl);
    }

图片转代码服务由CSDN问答提供 功能建议

我想将 data-rowindex 值动态保存到数组 <前> &lt; tr class =“ewTableRow”data-rowindex =“1”id =“r1_assessment_training”data-rowtype =“2”&gt; n

我试过下面的代码。 (我不确定代码下面的天气是否正确)?

  var a = $('#tbl_assessment_traininggrid tr')。length; 
 
var row_count = [  ]; 
 for(var i = 1; i&lt; = a; i ++){
 var rowCntrl = $(“data-rowindex [i]”)。val(); 
 alert(rowCntrl); 
  row_count.push(rowCntrl); 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • du0204 2018-02-07 04:48
    已采纳

    The below answer works for me.

    $(".ewTableRow").each(function(){
       row_count.push($(this).data('rowindex'));
    });
    

    Use Jquery function..!

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongxiang3648 2017-10-13 04:58

    Also please note that if you want to select an element with the variable i, then use $("[data-rowindex=" + i + "]")

    var a = $('#tbl_assessment_traininggrid tr').length;
    
    var row_count = [];
    for (var i = 1; i <= a; i++) {
      var rowCntrl = $("[data-rowindex=" + i + "]").attr("data-rowindex");
      alert(rowCntrl);
      row_count.push(rowCntrl);
    }
    
    console.log(row_count)
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table id="tbl_assessment_traininggrid">
      <tr class="ewTableRow" data-rowindex="1" id="r1_assessment_training" data-rowtype="2"></tr>
      <tr class="ewTableRow" data-rowindex="2" id="r2_assessment_training" data-rowtype="2"></tr>
      <tr class="ewTableRow" data-rowindex="3" id="r3_assessment_training" data-rowtype="2"></tr>
    </table>

    </div>
    
    评论
    解决 无用
    打赏 举报
  • duanguangwang5829 2017-10-13 05:11

    You can use each from Jquery to traverse over the elements and use .data() to get the data from the data attributes of the element (note: using data helps parse a json string and get a object directly from data attribute, incase you need to save a json string in data attribute), as shown below:

    var row_count = [];
        
    $(".ewTableRow").each(function(){
      row_count.push($(this).data('rowindex'));
    });
    
    console.log(row_count);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
       <tr class="ewTableRow" data-rowindex="1"  data-rowtype="2"></tr>
       <tr class="ewTableRow" data-rowindex="2"  data-rowtype="2"></tr>
       <tr class="ewTableRow" data-rowindex="3" data-rowtype="2"></tr>
      </table>

    </div>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题