我正在寻找一种让用户复制一个多行地址粘贴到一个网页中,每个地址行有一个输入字段,这样不仅第一行被粘贴,而且程序自动跳转到下一个字段并将第2行放在那里,等等。将多个文本行粘贴到多个html输入框中
下面是我好不容易才找到的代码,但它仅限于字数。在下面的代码是1 ..有什么办法这不会被一字涨停数但线数
function handleCharacter(event) {
var $input = $(this),
index = getIndex($input),
digit = $input.val().slice(0,1),
rest = $input.val().slice(1),
$next;
if (rest.length > 0) {
$input.val(digit); // trim input value to just one character
$next = $('.text-input[name="chars['+ (index + 1) +']"]');
if ($next.length > 0) {
$next.val(rest); // push the rest of the value into the next input
$next.focus();
handleCharacter.call($next, event); // run the same code on the next input
}
}
}
function handleBackspace(event) {
var $input = $(this),
index = getIndex($input),
$prev;
// if the user pressed backspace and the input is empty
if (event.which === 8 && !$(this).val()) {
$prev = $('.def-txt-input[name="chars['+ (index - 1) +']"]');
$prev.focus();
}
}
function getIndex($input) {
return parseInt($input.attr('name').split(/[\[\]]/)[1], 10);
}
$('.def-txt-input')
.on('keyup', handleCharacter)
.on('keydown', handleBackspace);
在此先感谢您的帮助!
2017-04-20
Guru
+0
也许你会发现[这](http://stackoverflow.com/questions/10950538/how-to-detect-line-breaks-in-a-text-area-input)有用吗? –