简体   繁体   English

如何使用jQuery(或香草JS)在标签上创建新输入

[英]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.

相关问题 我将如何在 vanilla JS 中执行此 ajax jquery? - How would I do this ajax jquery in vanilla JS? 如何在Vanilla JS中使用Apollo Client创建GraphQL订阅 - How do I create a GraphQL subscription with Apollo Client in Vanilla JS 我如何使用香草JS创建引导进度栏,如果不是香草的话 - how do I create bootstrap progress bar using vanilla JS and if not vanilla than something 如何在不使用 Angular 的情况下启动新的 Ionic 4 应用程序? 使用 Vue.js 或 Vanilla JS - How do I start a new Ionic 4 app without using Angular? With Vue.js or Vanilla JS 我如何使用键盘输入进行精灵更改我使用的是 vanilla JS - How do i Make a sprite change with keyboard input i'm using vanilla JS 当我删除不同的元素时,如何模拟单击输入复选框? (香草Js解决方案) - How do i simulate click on input checkbox when i remove a different element? ( Vanilla Js Solution) 如何在单击按钮时更改div的文本内容? 香草JS / jQuery - How do I change a div's text content on button click? Vanilla JS/Jquery 如何使用 vanilla JS 为单个文本行创建左右边距? - How do I create left and right margins for individual lines of text with vanilla JS? 如何使用 vanilla JS 或 Jquery 定位按钮以查看用户是否选择了它? - How do I target a button using vanilla JS or Jquery to see whether the user has selected it or not? 我如何将 vanilla Js 代码转换为 vue js 代码 - How do i convert vanilla Js code into vue js code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM