admin管理员组文章数量:1404469
I have a input type text
where the users enter numeric codes separated by ,
or by -
(to make a range). I'd like to allow the user of my site to paste a codes list. I've already managed to bind paste event (with jQuery) and parse the input string removing spaces and everything.
The problem starts when the user codes list is multiline. I haven't found any way to manipulate that text before the browsers attempts to insert it into the input, so the string is truncated at the end of the first line. Is there any way to manipulate the string before the browser truncates it?
Thanks!
Update here there is an example with JSFiddle... Stupid IE, in FF this works great.
I have a input type text
where the users enter numeric codes separated by ,
or by -
(to make a range). I'd like to allow the user of my site to paste a codes list. I've already managed to bind paste event (with jQuery) and parse the input string removing spaces and everything.
The problem starts when the user codes list is multiline. I haven't found any way to manipulate that text before the browsers attempts to insert it into the input, so the string is truncated at the end of the first line. Is there any way to manipulate the string before the browser truncates it?
Thanks!
Update here there is an example with JSFiddle... Stupid IE, in FF this works great.
Share Improve this question edited Mar 2, 2012 at 15:12 Diego asked Mar 2, 2012 at 15:02 DiegoDiego 16.7k26 gold badges88 silver badges136 bronze badges 3- Can you show a jsFiddle, please? – Ry- ♦ Commented Mar 2, 2012 at 15:05
- Can you provide a sample? I tested this this within a JSfiddle without any problem. – Smamatti Commented Mar 2, 2012 at 15:06
- I've just made the example... it is the stupid IE – Diego Commented Mar 2, 2012 at 15:12
3 Answers
Reset to default 2I built a plugin to replace the input for a <textarea>
. Here it is:
// Debe llamarse al plugin antes de construir cualquier referencia al elemento.
// El nuevo elemento debe seleccionarse por id, por name o por clases.
// En el momento de llamar al plugin el elemento debe estar visible.
// Translate:
// The plugin must be called before saving any reference to the element.
// The new element must be selected by its id, name or classes, not by the tag "input".
// When the plugin is called the element must be visible.
$.fn.acceptMultiline = function() {
var txt = $(this),
txtArea = $("<textarea></textarea>");
if (txt.attr("style") != undefined)
txtArea.attr("style", txt.attr("style"));
if (txt.attr("class") != undefined)
txtArea.attr("class", txt.attr("class"));
if (txt.attr("name") != undefined)
txtArea.attr("name", txt.attr("name"));
if (txt.attr("id") != undefined)
txtArea.attr("id", txt.attr("id"));
txtArea
.width(txt.width())
.height(txt.height())
.css("resize", "none");
txt.after(txtArea)
.remove();
txtArea.on("input", function() {
txtArea.val(txtArea.val().replace(/\r\n/g, " ").replace(/\r/g, " ").replace(/\n/g, " "));
});
};
$(":text").acceptMultiline();
$("button").on("click", function() {
$(".txt").val($(".txt").val() + "pepe");
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<input type="text" style="width: 700px;" class="txt" />
<button>Change value of txt</button>
View on JSFiddle
Convert the string to one line by remove the line breaks.
"multiline\ntext\nline3".split('\n').join('');
Check this SO thread. It links to a jsfiddle that has code that i think will handle your problem
Is it possible to get pasted text without using the setTimeout() function?
The link to the jsfiddle http://jsfiddle/pxfunc/KDLjf/
本文标签: javascriptPaste multiline text in input type text (not textarea)Stack Overflow
版权声明:本文标题:javascript - Paste multiline text in input type text (not textarea) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744802248a2625932.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论