2012-08-29 37 views
2

我有这样的一个表:变化​​元素值

Column 1 | Column 2 
------------------- 
Textbox1 | Text1 
Textbox2 | Text2 

我想改变Textbox1值,当光标移出它将运行change事件到Text1值替换到Success

<html> 
    <head> 
     <title>Demo - Change Value</title> 
     <script type="text/javascript" language="javascript" src="js/jquery.js"></script> 
     <script> 
      $(document).ready(function() { 
       $('#example table tbody tr td input').change(function() { 
        var rowEdit = $($(this).parents('tr').html()); 

        rowEdit.closest('.sub').html('Success'); 
<!--     $('.sub').html('Change');--> 
       }) 
      }) 
     </script> 
    </head> 

    <body> 
     <div id="example"> 
      <table> 
       <thead> 
        <th>Column 1</th> 
        <th>Column 2</th> 
       </thead> 
       <tbody> 
        <tr> 
         <td><input type="text" /></td> 
         <td class="sub">123</td> 
        </tr> 
        <tr> 
         <td><input type="text" /></td> 
         <td class="sub">456</td> 
        </tr> 
       </tbody> 
      </table> 
     </div> 
    </body> 
</html> 

的问题是,价值并没有改变我叫rowEdit.closest('.sub').html('success');之后,但它会改变Text1Text2如果我用$('.sub').html('Change');

我的目标是更改列2中的文本,当我更改第1列的值input与同一行。我怎样才能做到这一点?

+0

增加了一个答案。 – insomiac

回答

4

试试这个:

$(document).ready(function() { 
    $('#example table tbody tr td input').change(function() { 
    var rowEdit = $(this).parents('tr'); 
    rowEdit.children('.sub').html('Success'); 
    }) 
}) 

既然你获取父TR元素,儿童()应该做的伎俩。

这里是jsFiddle

+2

不需要将'rowEdit'重新包装为jQuery对象。您已经在初始变量实例化中引用它。 – Ohgodwhy

+0

谢谢@Ohgodwhy,更新了代码。 –

+0

@SenthilKumar:谢谢,它的工作原理。 – Crazenezz

1

试试这个.... FIDDLE

$('#example input').change(function() { 
    $(this).closest('tr').find('td.sub').html('Success'); 
});