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的后史来判断。可能发生的情况是,他已经有一个“