terça-feira, 31 de março de 2009

[snippet] Adicionando inputs ao pressionar enter

Inserindo mais inputs no enter:

adminEssays.form = {};

adminEssays.form.addInput = function(target) {
var nextInput;
inputs = $("#category-subs").children('input');
if(inputs.length == 0) {
nextInput = 0;
}
else {
nextInput = 0;
for (var a = 0; a < inputs.length; a++) {
id = parseInt(inputs[a].id.substr(21));
if (id >= nextInput) {
nextInput = id + 1;
}
}
}
id = "category-subcategory-" + nextInput;
html = "< input type='text' size='70' id='" + id + "' class='category-subcategory' name='subcategory[]' style='display:block'/>";
if(nextInput == 0) {
$('#category-subs').html(html);
}
else {
$(target).after(html);
}
$(".category-subcategory").bind("keypress",adminEssays.form.inputEnter);
}

adminEssays.form.submitForm = function(id) {
JsonC.submitForSfAction('adminEssays/insertSubcategories',{},'form-insert-subcategories','form-insert-subcategories-loading');
}

adminEssays.form.inputEnter = function(evt) {
if(evt.keyCode == 8 || evt.keyCode == 46) {
var tgt = $(evt.currentTarget);
if(tgt.val() == '') {
if( $('#category-subs').children().length > 1 ) {
var inputToFocus;
if(tgt.next().length > 0) {
inputToFocus = tgt.next()[0];
}
else {
inputToFocus = tgt.prev()[0];
}
tgt.remove();
inputToFocus.focus();
}
return false;
}
}
if(evt.keyCode == 13) {
prox = $(evt.currentTarget).next().length;
if(prox > 0) {
$($(evt.currentTarget).next()[0]).focus();
}
else {
adminEssays.form.addInput(evt.currentTarget);
$($(evt.currentTarget).next()[0]).focus();
}
}
};

Nenhum comentário:

Postar um comentário