2014-03-25 54 views
0

我有一個表格,我需要獲取td的文本,我已經點擊並將其打印在輸入框中。 現在我已經有了這段代碼,但它打印了表格的最後一個值。從td獲取文本並在輸入框中打印

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

對於什麼是。每個()? – zvona

回答

1

只是刪除每個,你不需要它!

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

你可以看到這裏的解決方案:http://jsfiddle.net/GNkxm/

+0

謝謝,它的工作=)我在JavaScript中太糟糕了哈哈 – temerariomalaga

1

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

所以,這將是enough-

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

    $("#prueba").val(($(this).text())); 
}); 
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 
}); 
相關問題