从td获取文本并将其打印在输入中

时间:2014-03-25 11:36:24

标签: javascript html-table

我有一张桌子,我需要获取我点击的td文本并将其打印在输入框中。 现在我有了这段代码,但它打印了表格的最后一个值。

$("td").click(function(){  
    $("td").each(function(index){
        $("#prueba").val(($(this).text())); 
    }); 
}); 

3 个答案:

答案 0 :(得分:1)

您可以删除$("td").each(function(index){

所以,这就够了 -

$("td").click(function(){  

    $("#prueba").val(($(this).text())); 
}); 

答案 1 :(得分:1)

对于每个循环,您assigning the text of each Td block为ID为prueba的元素,最后它具有最后cell(td)的值

$("td").click(function(){  
    //$("td").each(function(index){ // not needed
        $("#prueba").val(($(this).text())); 
    //}); // not needed
}); 

答案 2 :(得分:1)

只需删除每个,你不需要它!

$("td").click(function(){  
    $("#prueba").val(($(this).text()));
}); 

你可以在这里看到解决方案:http://jsfiddle.net/GNkxm/

相关问题