2012-07-31 56 views
1

这是我想要做的。获取表格行数据td并选择

在第一行有TDS然后a = the text in the first cellb = the selected value of the drop down that the user selected

我怎么做,因为我的代码不能正常工作?

$(document).ready(function() { 
    var s = $('table td:first').parents('tr'); 

    var a = s.eq(0).text(); 
    var b = s.eq(1).find('option:selected').val(); 

    alert(a + " " + b); 
}); 

<table> 
    <tbody> 
     <tr> 
     <th>ID</th> 
     </tr> 
     <tr> 
      <td> 
      test 
      </td> 
      <td> 
      <select> 
       <option value="yes">yes</option> 
       <option selected="selected" value="no">no</option> 
      </select> 
      </td> 
     </tr> 
    </tbody> 
</table> 
+0

查阅这些链接希望它可以帮助你... HTTP://stackoverflow.com/questions/5866862/how-to-get-the-data-from- a-row-jquery http://stackoverflow.com/questions/10147971/jquery-getting-values-from-selected-table-row http://forums.asp.net/t/1652535.aspx – 2012-07-31 09:37:26

回答

2

工作演示http://jsfiddle.net/snU97/

休息随意玩耍,&希望它可以帮助您的需求:)

代码

$(document).ready(function() { 

    var s = $('table td:first').parents('tr'); 

    var a = s.find('td').eq(0).text();//s.eq(0).text(); 
    var b = s.find('td').eq(1).find('option:selected').val(); 

    alert(a + " " + b); 

});​ 
+1

你打我它。 http://jsfiddle.net/Qpirate/k5sPC/ – Qpirate 2012-07-31 09:39:51

+0

@Qpirate':)'〜 – 2012-07-31 09:40:31

1

你也可以使用下面的代码

$(文件)。就绪(函数(){

var s = $('table td:first'); 

var a = s.html(); 
var b = s.parents('tr').children().find('option:selected').val(); 

alert(a + " " + b); 

});

1

有一个选项:

<table> 
     <tbody> 
      <tr> 
       <th>ID</th> 
      </tr> 
      <tr> 
       <td> 
        test 
       </td> 
       <td> 
        <select id="mydropdown"> 
         <option value="yes">yes</option> 
         <option selected="selected" value="no">no</option> 
        </select> 
       </td> 
      </tr> 
     </tbody> 
    </table>​​​ 



$(document).ready(function() { 

    var s = $('table td:first'); 

    var a = s.text(); 
    var b = $("#mydropdown option:selected").text(); 

    alert(a + " " + b); 

});​