开发者

Setting Label Text to Number of Rows with jQuery

This is my code but nothing happens.

$(document).ready(function() {
  var totalRows = $("#<%=GridView3.ClientID %> tr").length;
  document.getElementById('Label6').InnerHTML = tot开发者_如何学JAVAalRows;
});

this is what i ended up using

var rqnum = $("#<%=GridView3.ClientID %> tr").length - 1;
document.getElementById('rqnum').innerHTML = rqnum;

var oknum = $("#<%=GridView4.ClientID %> tr").length - 1;
document.getElementById('oknum').innerHTML = oknum;

var xlnum = $("#<%=GridView5.ClientID %> tr").length - 1;
document.getElementById('xlnum').innerHTML = xlnum;

var dynum = $("#<%=datalist1.ClientID %> tr").length / 3;
document.getElementById('dynum').innerHTML = dynum;

this way it subtracts the header, however, how can i make it so if it is 0 dont subtract the -1 because i dont want negatives


With jQuery, I believe you should be able to set it this way:

$("#Label6").html(totalRows);


It is innerHTML.

or if you want to use jquery you can simply do this:

$("#Label6").text(totalRows);


Try .innerHTML rather than .InnerHTML. Make sure to always use the proper case.

Also, since you're working with jQuery you could ignore the .innerHTML and simply set the value of #Label6 using either $.html() or $.text().

Lastly, be sure that <%=GridView3.ClientID %> is producing the output that you are expecting.

Updates following question in comment

If you would like to ignore your table headers, you could count only the tr elements within the tbody tag:

<table id="myTable">
  <thead>
    <tr>
      <td>Column 1</td>
      <td>Column 2</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Foo</td>
      <td>Bar</td>
    </tr>
  </tbody>
</table>

<script>
  $(function(){
    $("#Label6").text( $("#myTable tbody tr").length );
  });
</script>

Preventing a negative number is fairly simple as well. You could use a ternary operator to do this:

var numRows = ( numRows < 0 ) ? 0 : numRows ;

Perform this logic before using the numRows variable and it will prevent any negative number from showing up. The lowest number to show will be 0.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜