2015-02-23 23 views
0

有兩個文本格式完全相同的textarea。 ID是textareaAtextareaB使用鍵盤輸入時調整textarea寬度

我已經設置了一些東西發生textareaA密碼。我的問題是如何設置textareaB在用戶輸入textareaA時調整其WIDTH大小?

這是我試過但沒有運氣。

$(document).on("click blur keyup", ".fT", function() { // keyup responds with textareaA 
     var newWidth = $("#textareaA").val(); //does not work 
     $("#textareaB").width(newWidth); 
}); 
+0

這裏有http://stackoverflow.com/questions/454202/creating-a-textarea-with-auto-resize – Alexander 2015-02-23 12:48:50

+0

@Alexander一些偉大的答案:謝謝,那是關於scrollHeight屬性。我正在尋找寬度。 – Fergoso 2015-02-23 12:49:42

回答

0

使用.css("proprety", "value") JQuery方法執行此操作。

用法:

$(document).keyup(function(event)) { 
    $("#textareaB").css("width", "1000px"); 
}); 

用法(可變):

var myVariable = 1000; /* integer here */ 

$(document).keyup(function(event)) { 
    $("#textareaB").css("width", myVariable.toString() + "px"); 
}); 
+0

謝謝,這有一個固定的寬度? – Fergoso 2015-02-23 12:52:56

+0

我設置了一個固定的寬度,但你可以使用一個變量,但我不認爲它可以用整數運行,你應該首先將該變量轉換爲字符串。 – 2015-02-23 12:54:10

+0

這與我的問題類似嗎?那麼...你如何建議我得到價值取代1000px? – Fergoso 2015-02-23 13:00:52

0
$('#TextAreaId').keyup(function(e) { 
     while ($(this).outerHeight() < this.scrollHeight + parseFloat($(this).css("borderTopWidth")) + parseFloat($(this).css("borderBottomWidth"))) { 
      $(this).height($(this).height() + 1); 
     }; 
    }); 

編輯2:(增加textarea的寬度)

注::改變值'8'如果根據你的需要和最適合的

$(function() { 
$('#mta').keyup(function(e) { 

    console.log($(this).outerWidth()+'--'+$(this).val().length*8); 
     if($(this).outerWidth() > $(this).val().length) { 
      $(this).width($(this).width() + 1); 
     }; 
    }); 

}); 
+0

謝謝。這調整高度?我正在尋找一種可以調整寬度的解決方案。 – Fergoso 2015-02-23 12:59:05

+0

增加高度是正確的方法,爲什麼寬度? – 2015-02-23 13:05:17

+0

我的要求是將textarea的寬度設置爲其文本的長度。例如:如果有6個單詞或2個單詞的句子,我希望textarea只能達到文本的長度。我想避免任何不必要的textarea長度。 – Fergoso 2015-02-23 13:10:39