2013-04-18 39 views
0

這是第四位的輸入域:獲得的價值構成另一種輸入字段

<td><input type="number" name="tour" value="12" size="2" maxlength="2"></td> 

這個js dosnt工作:

var hTour = $(this).parents('tr').find('td:nth-child(4)').val(); 

的哪些錯誤?

+1

那麼'this'指的是什麼? – tymeJV

+0

'td'標籤沒有'value'屬性。 –

回答

2

您的JavaScript正在獲取td元素的值。你可能希望input元素的值:

var hTour = $(this).closest('tr').find('td:nth-child(3) input').val(); 

:nth-child()爲零指數的,所以:nth-child(0)是第一要素,:nth-child(1)是第二個,依此類推。

0
與您的代碼

其實:

var hTour = $(this).parents('tr').find('td:nth-child(4)').val(); 

你正在尋找的td內的值。但是,你需要輸入的td喜歡裏面的值:

var hTour = $(this).parent('tr').find('td:nth-child(4) input').val(); 

:nth-child() Selector API docs

指數:每個孩子的指數相匹配,從1開始,字符串 偶數或奇數或等式(例如:第n個孩子(偶數),:第n個孩子(4N))

另外

Given a single <ul> containing two <li>s, $('li:nth-child(1)') selects 
the first <li> while $('li:eq(1)') selects the second.