你可以这样做,
$('form input').on("propertychange keyup input paste", addInput);
function addInput() {
var remainingChars = $(this).val().length;
if (remainingChars == 24) {
$('<input type="text">').change(fileChangeHandler).appendTo('form').focus().on("propertychange keyup input paste",addInput);
}}
http://jsfiddle.net/5KZtB/
编辑 - 存储输入值
要将字段的值存储在数组中,您可以在创建新字段之前推送每个值。例如,
jQuery(function($) {
var values = [];
$('form input[type=text]').change(fileChangeHandler);
function fileChangeHandler() {
var form = $(this).closest('form');
}
var form = $(this).closest('form');
$('form input').on("propertychange keyup input paste",
addInput);
function addInput() {
var remainingChars = $(this).val().length;
if (remainingChars == 24) {
$('<input type="text">').change(fileChangeHandler).appendTo('form').focus().on("propertychange keyup input paste",addInput);
values.push($(this).val());
//console.log(values);
}}
});
http://jsfiddle.net/5KZtB/1/
EDIT2 - 只允许数字
....
$('form input').on("propertychange keyup input paste",
addInput);
onlyNums($('form input'));
function addInput() {
var remainingChars = $(this).val().length;
if (remainingChars == 24) {
var newInput = $('<input type="text">').change(fileChangeHandler).appendTo('form').focus().on("propertychange keyup input paste",addInput);
onlyNums(newInput);
values.push($(this).val());
console.log(values);
}}
function onlyNums($elem){
$elem.keydown(function(event) {
// Allow only backspace and delete
if ( event.keyCode == 46 || event.keyCode == 8 ) {
// let it happen, don't do anything
}
else {
// Ensure that it is a number and stop the keypress
if (event.keyCode < 48 || event.keyCode > 57 ) {
event.preventDefault();
}
}
});
}
http://jsfiddle.net/5KZtB/2/
EDIT3 - 删除输入元素的字段
一种粗略的方法可能是在 span 元素内添加输入以及带有 x 标记的链接以删除特定输入。所以代码稍作修改如下,
HTML
<form>
<span class="input-component"><input type="text"/><a href=#>[x]</a></span>
</form>
JS
jQuery(function($) {
var values = [];
$('form input[type=text]').change(fileChangeHandler);
function fileChangeHandler() {
var form = $(this).closest('form');
}
var form = $(this).closest('form');
$('form .input-component input').on("propertychange keyup input paste",
addInput);
onlyNums($('form .input-component input'));
initializeDelLink($('form .input-component'));
function addInput() {
var remainingChars = $(this).val().length;
if (remainingChars == 24) {
var $newInputComponent = $('<span class="input-component"><input type="text"/><a href=#>[x]</a></span>').change(fileChangeHandler).appendTo('form').find('input').focus().on("propertychange keyup input paste",addInput).end();
initializeDelLink($newInputComponent);
var $newInput = $newInputComponent.find('input');
onlyNums($newInput);
values.push($(this).val());
console.log(values);
}}
function initializeDelLink($elem){
var $delLink=$elem.find('a');
$delLink.click(function(){alert();
$(this).parent('.input-component').remove();
});
}
function onlyNums($elem){
$elem.keydown(function(event) {
// Allow only backspace and delete
if ( event.keyCode == 46 || event.keyCode == 8 ) {
// let it happen, don't do anything
}
else {
// Ensure that it is a number and stop the keypress
if (event.keyCode < 48 || event.keyCode > 57 ) {
event.preventDefault();
}
}
});
}
});
http://jsfiddle.net/5KZtB/3/