2012-11-02 34 views
1

我從數據庫中生成一個表,看起來像這樣的jQuery上點擊同胞選擇

<table id="items"> 
<thead> 
    <tr> 
     <th>Details</th> 
     <th>Goldmine ID</th> 
     <th>&nbsp;</th> 
    </tr> 
</thead> 
<tbody> 
    <tr> 
     <td class="evenrow">This is a test Description generated through UNIT Tests for the category description</td> 
     <td class="evenrow"><input type="text" value="" id="106" class="gminput"></td> 
     <td class="butCell evenrow"><button class="saveButton updateitem">Update</button></td> 
    </tr> 
    <tr> 
     <td class="oddrow">This is a test Description generated through UNIT Tests for the category description</td> 
     <td class="oddrow"><input type="text" value="" id="107" class="gminput"></td> 
     <td class="butCell oddrow"><button class="saveButton updateitem">Update</button></td> 
    </tr> 
    <tr> 
     <td class="evenrow">This is a test Description generated through UNIT Tests for the category description</td> 
     <td class="evenrow"><input type="text" value="" id="108" class="gminput"></td> 
     <td class="butCell oddrow"><button class="saveButton updateitem">Update</button></td> 
    </tr> 
</tbody> 
</table> 

我試圖讓輸入框的值和ID由相關行的按鈕返回點擊

至今我曾經試過,但失敗了

$('body').on('click', '.updateitem', function(event) { 
    event.preventDefault(); 
    $(this).parent().siblings().each(function(index) { 
     alert(($(this).val())); 
    }); 
    var par = sib.parent('td'); 
    par.addClass('redBorder'); 
}); 
+0

我可以將你的問題總結爲一句話;) – user1534664

+0

作爲一個便箋,它是'.each'。 – pimvdb

+1

@ user1534664就我個人而言,我已經閱讀了整篇文章,並且我希望有足夠的信息來確定需要什麼。 HTML特別有用。 –

回答

4

你想要的元素是

$(this).closest('tr').find('.gminput') 

可以使用

var input = $(this).closest('tr').find('.gminput'); 
var value = input.val(); 
var id = input.attr('id'); 
+0

謝謝我會在2分鐘內接受lol – Deviland

0

有jQuery中沒有一種方法forEach() ..嘗試獲得的價​​值和id .each()

$(this).parent().parent().each(function(index) { 
    if(!$(this).hasClass('butCell')) { 
     if($(this).find('input').length > 0) { // we have to verify if tds has text inputs or not 
      alert($(this).find('input').val()); 
     } else { 
      // if there is no text input we get the text inside td 
      alert($(this).text()); 
     } 
    } 
}); 
+0

是的,我得到這個我很快就寫了,通常用C#工作原諒我的錯誤 – Deviland

0
$('.updateitem').on('click', function(){ 
    $elem = $(this).parents('tr').find('.gminput'); 
    var id = $elem.prop('id'); 
    var val = $elem.val() 
}) 

爲什麼對body click事件,當button可能就足夠了?

+0

我猜'是動態生成的。 – pimvdb

+0

這應該仍然有效,我想。 – Abhilash

+0

如果行是動態生成的,仍然會遇到問題。 – pimvdb