[英]How do I create a new input on tab with jQuery (or vanilla JS)
Like many things, my question is best explained with a picture: http://gyazo.com/f7df971c91a5ab4a4969d12f4c5f32b6.png?1329575807 像许多事情一样,最好用一张图片来解释我的问题: http : //gyazo.com/f7df971c91a5ab4a4969d12f4c5f32b6.png?1329575807
When the user is on the last field in each column, I want a new input to pop up underneath the current input when they press tab (similar to what Quizlet does). 当用户位于每一列的最后一个字段时,我希望在他们按下Tab键时在当前输入下方弹出一个新输入(类似于Quizlet所做的事情)。
I don't have that much experience with jQuery, could someone help me out, or point me in the right direction? 我在jQuery方面没有太多经验,有人可以帮我一下,还是向我指出正确的方向?
EDIT: So here's what I have currently: 编辑:所以这是我目前所拥有的:
$(document).ready(function(){
$('section#attendance input:last').keydown(function(e) {
if (e.which == 9)
{
var $this = $(this);
var num = parseInt($this.attr('name').match(/\d+(,\d+)?/)[0]) + 1;
$this.parent().append('<input type="text" name="attendance[' + num +']" value="Name and Position" class="medium" />');
}
});
}); });
For some reason, after it creates the first new input, it doesn't use that new input for the tab trigger, but rather the input it used before. 由于某种原因,在创建第一个新输入后,它不会将新输入用作选项卡触发器,而是将其先前使用的输入使用。 Any idea why?
知道为什么吗?
$('input[type="text"]:last').live('keyup',
function(e) {
if (e.which == 9) {
$(this).parent().append($('<input/>', {
value: '',
type: 'text',
name: 'YOUR_CHOICE',
class: 'YOUR_CHOICE'
}));
}
});
Assuming you have the column fields in some contanier (eg a 'column' div) and the fields have class field
, and the last field has class last
, then this might help: 假设您在某个容器中有column字段(例如'column'div),并且这些字段具有class
field
,而last字段具有last
类,那么这可能会有所帮助:
$('.last').live('focusout', function() {
var $this = $(this);
$this.removeClass('last');
$this.parent().append('<input class="field last" type="text"></input>');
});
Of course, you could append something a lot prettier than that ;) 当然,您可以添加比这还漂亮的东西;)
function KeyDown(e) {
if (e.which == 9) { // tab
// Do your stuff
}
}
$(document).keydown(KeyDown);
In your case you'll probably not want to bind to document, but to the last element. 在您的情况下,您可能不希望绑定到document,而是最后一个元素。 Or you could do that focus check inside the handler method.
或者,您可以在处理程序方法中进行焦点检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.