每一行的第一列有一个复选框,现在是当你单击复选框的时候,怎么通过行号获取到该行的id的属性值呢

解决方案 »

  1.   

    点击可以获得复选框。则可以通过parentNode  childNodes来获得
      

  2.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
     <head>
      <title> New Document </title>
      <meta name="Generator" content="EditPlus">
      <meta name="Author" content="">
      <meta name="Keywords" content="">
      <meta name="Description" content="">
      <script type="text/javascript">
      <!--
    function showVal(obj){
    var val = obj.parentNode.parentNode.children[1].innerHTML;
    alert(val);
    }
      //-->
      </script>
     </head> <body>
      <table border="1">
      <tr>
    <td>&nbsp;</td>
    <td>id</td>
    <td>name</td>
      </tr>
      <tr>
    <td><input type="checkbox" onclick="showVal(this)"></td>
    <td>1</td>
    <td>lisi</td>
      </tr>
      <tr>
    <td><input type="checkbox" onclick="showVal(this)"></td>
    <td>2</td>
    <td>lisi2</td>
      </tr>
      <tr>
    <td><input type="checkbox" onclick="showVal(this)"></td>
    <td>3</td>
    <td>lisi3</td>
      </tr>
      </table>
     </body>
    </html>
      

  3.   

    不好意思  我补充一句   是获得复选框本行tr的属性id的值    
      

  4.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script type="text/javascript">
    window.onload = function() {
    var obj = document.getElementsByTagName('body')[0].getElementsByTagName('input');
    for(x in obj) {
    obj[x].onclick = function() {
    alert(this.parentNode.parentNode.id);
    }
    }
    }
    </script>
    </head><body>
    <table width="100%" border="0">
        <tr id="tr1">
            <td><input type="checkbox" /></td>
            <td>&nbsp;</td>
        </tr>
        <tr id="tr2">
            <td><input type="checkbox" /></td>
            <td>&nbsp;</td>
        </tr>
    </table>
    </body>
    </html>
      

  5.   

    问题是,这个checkbox和tr是没有什么直接关系的
      

  6.   

    把checkbox的id值置为和所在行的id值一样,获取checkbox就等于获取了当前行,这样处理就方便了
    因为id不能重复,可以写成<tr id='trid'><td><input type='checkbox' id='cheid' /></td></tr>,然后把获取到的checkbox的id值用js处理一下
      

  7.   

    这是我点击复选框触发的函数
    function showindex(){
       currobj = event.srcElement.parentElement.parentElement;
       if(currobj.tagName == "TR") {
         currindex = currobj.rowIndex;
       }
    }通过var id1=currobj.id;  就可以得到该行的id属性值了
    谢谢   
      

  8.   

    srcElement、parentElement都是IE Only的。
      

  9.   

    6楼的代码不能满足你的要求?要获取行号的话直接this.parentNode.parentNode.rowIndex即可:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script type="text/javascript">
    window.onload = function() {
        var obj = document.getElementsByTagName('body')[0].getElementsByTagName('input');
        for(x in obj) {
            obj[x].onclick = function() {
                alert(this.parentNode.parentNode.id);
        alert(this.parentNode.parentNode.rowIndex);
            }
        }
    }
    </script>
    </head><body>
    <table width="100%" border="0">
        <tr id="tr1">
            <td><input type="checkbox" /></td>
            <td>&nbsp;</td>
        </tr>
        <tr id="tr2">
            <td><input type="checkbox" /></td>
            <td>&nbsp;</td>
        </tr>
    </table>
    </body>
    </html>