2012-11-02 37 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