0
我正在尋找一種讓用戶複製一個多行地址粘貼到一個網頁中,每個地址行有一個輸入字段,這樣不僅第一行被粘貼,而且程序自動跳轉到下一個字段並將第2行放在那裏,等等。將多個文本行粘貼到多個html輸入框中
下面是我好不容易纔找到的代碼,但它僅限於字數。在下面的代碼是1 ..有什麼辦法這不會被一字漲停數但線數
<input type="text" class="text-input" name="box1">
<input type="text" class="text-input" name="box2">
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);
在此先感謝您的幫助!
也許你會發現[這](http://stackoverflow.com/questions/10950538/how-to-detect-line-breaks-in-a-text-area-input)有用嗎? – PHPglue