2012-07-31 55 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); 

});​