2014-09-01 84 views
0

我試圖在用戶單擊文本區域後實現重新調整文本區域大小的效果。出於某種原因,以下代碼僅適用於數據表的第一行。數據表是動態生成的,並顯示來自數據庫查詢的數據。在動態生成的表格中重新調整文本區域onFocus和jQuery

流動的代碼生成表格,文本區域字段id=txtArea代碼:

echo "<td ><textarea size=4 name=WinterFollowUp id='txtArea' rows=2  cols=12 >$row[WinterFollowUp]</textarea></div> </td>"; 

這是jQuery的腳本,我有:

$(document).ready(function(){ 
    $('#txtArea').focus(function(){ 
     $(this).attr('rows', '10'); 
    }); 
}); 
$('#txtArea').blur(function(){ 
    $(this).attr('rows', '2'); 
}); 

回答

0

的ID是jQuery中獨樹一幟,所以jquery會找到第一個帶有該id的textarea,如果你想把 應用到所有的textarea你必須指定一個類或只有textarea

$(document).ready(function(){ 
    $('textarea').focus(function(){ 
    $(this).attr('rows', '10'); 
    }); 
}); 
$('textarea').blur(function(){ 
    $(this).attr('rows', '2'); 
});