5
我有一行文本框。使用jquery將文本框轉換爲多行
我想用jQuery將其轉換爲多行,但控制添加到它的行數。我也希望能夠限制每行的字符數。
任何想法,我可以做到這一點與jQuery的?
編輯:
是我的意思的文本框爲<input type="text">
EG。 <input type="text" value="" name="txtTextBox1" id="xtTextBox1">
我有一行文本框。使用jquery將文本框轉換爲多行
我想用jQuery將其轉換爲多行,但控制添加到它的行數。我也希望能夠限制每行的字符數。
任何想法,我可以做到這一點與jQuery的?
編輯:
是我的意思的文本框爲<input type="text">
EG。 <input type="text" value="" name="txtTextBox1" id="xtTextBox1">
考慮你有以下HTML:
<input type="text" class="myclasses" style="color: #123123" value="akira"/>
然後使用下面的代碼片段:
(function($) {
$.fn.changeToTextArea = function(rows, columns) {
var attrs = {};
var text = "";
$.each(this[0].attributes, function(idx, attr) {
attrs[attr.nodeName] = attr.nodeValue;
if(attr.nodeName == "value") {
text = attr.nodeValue;
}
attrs["rows"] = rows;
attrs["cols"] = columns;
});
this.replaceWith(function() {
return $("<textarea/>", attrs).append($(this).contents()).html(text);
});
}
})(jQuery);
你應該
$("input").changeToTextArea(7, 25);
(function($) {
$.fn.changeToTextArea = function(rows, columns) {
var attrs = {};
var text = "";
$.each(this[0].attributes, function(idx, attr) {
attrs[attr.nodeName] = attr.nodeValue;
if(attr.nodeName == "value") {
text = attr.nodeValue;
}
attrs["rows"] = rows;
attrs["cols"] = columns;
});
this.replaceWith(function() {
return $("<textarea/>", attrs).append($(this).contents()).html(text);
});
}
})(jQuery);
$("input").changeToTextArea(7, 25);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<input type="text" class="xyzxterms" style="color: #123131" value="akira"/>
術語「文本框」不夠具體。這似乎是ASP.NET特定的。 jQuery不理解這一點。它只能理解HTML。你是不是指HTML'或者'
@BalusC我會將它解釋爲將'input type = text'轉換爲'textarea'。我認爲這隻能通過銷燬輸入並創建一個同名的textarea來完成。 – 2010-08-25 18:26:39
@Pekka:是的,但是這個詞有些模棱兩可,我根據OP的後史來判斷。可能發生的情況是,他已經有一個「