下面是我的HTML代码这个TR是循环生成的会有多行
<tr>
                <td>
                    <input type="text" id="txtType" class="c_Type" />
                    <input class="btnType" id="Button1"/>
                </td>
                <td>
                    <input type="text" id="txtBrand" class="c_Brand" />
                </td>
                <td>
                    <input type="text" id="txtBatch" class="c_Batch" />
                </td>
                <td>
                    <input type="text" id="txtEncapsulation" class="c_Encapsulation" />
                </td>
                <td>
                 <input type="button" id="btnHistory" value="查看历史" class="c_History" />
                </td>
            </tr> 这里如果用ID来做选择器不行,只能用class我是这样写的 $(".c_History").click(function(){
   var type = $(this).parent("td").prev().children("[type=text]").val();
  这句只能获取到前一个TD 属于text的值
  而我想要的是点击按钮的时候获取前面几个TD的值
  就是 c_Type  c_Brand c_Batch  c_Encapsulation 这四个text的值
  要循环吗? 想了好久真做不出来, 希望大家JQ高手帮帮瞅一瞅  
});

解决方案 »

  1.   

     var arr=[];
    var aa = $(this).parent("td").prevAll().children("[type=text]").each(function(){ arr.push($(this).val())
    });写个小片段,没测!
    使用了PreAll返回数组,each迭代数组每个元素
      

  2.   

    我刚测 arr的长度是有的但是value值却是undefined
      

  3.   


    <!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>
    <title> new document </title>
    <SCRIPT LANGUAGE="JavaScript" src="http://localhost:3319/js/jquery.js"></SCRIPT>
    </head>
    <body>
    <table>
    <tr>
      <td>
      <input type="text" id="txtType" class="c_Type" value='c_Type' />
      <input type="text" class="btnType" id="Button1" value="button1" />
      </td>
      <td>
      <input type="text" id="txtBrand" class="c_Brand" value='c_Brand' />
      </td>
      <td>
      <input type="text" id="txtBatch" class="c_Batch" value="c_Batch" />
      </td>
      <td>
      <input type="text" id="txtEncapsulation" class="c_Encapsulation" value="c_Encapsulation" />
      </td>
      <td>
      <input type="button" id="btnHistory" value="查看历史" class="c_History" />
      </td>
      </tr>
    </table>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    $(".c_History").click(function(){
    $(this).parent().prevAll().children(":text").each(function()
    {
    alert(this.value);
    });
    });
    //-->
    </SCRIPT>
    </body>
    </html>