简体   繁体   English

导航到某个输入字段时,如何自动生成新的输入字段?

[英]How can I automatically generate a new input field when a certain input field is navigated to?

Inspired by the Chrome Postman extension , I want to implement a multi-field form section by automatically adding a new input field when a certain field is navigated to, and give focus to the new field. Chrome Postman扩展程序的启发,我想通过在导航到某个特定字段时自动添加新的输入字段并实现对新字段的关注,来实现多字段表单部分。 The below screenshot shows how this works in Postman; 下面的屏幕截图显示了Postman中的工作原理。 the downmost row contains a couple of inputs that, when navigated to, results in a new row being added above it, which can be typed into. 最下一行包含几个输入,当导航到这些输入时,会导致在其上方添加一个新行,可以将其键入。

邮差

How can I achieve this behaviour in JavaScript/jQuery? 如何在JavaScript / jQuery中实现此行为? To simplify matters, I need only one input field per row. 为简化起见,每行只需要一个输入字段。 I've created a fiddle that should serve as a starting point for a solution. 我创建了一个小提琴 ,应该作为解决方案的起点。

Example HTML: HTML示例:

<div id="last-row">
  <input name="multifield" placeholder="Value"></input>
</div>

See how I'd do it: http://jsfiddle.net/jCMc8/8/ 看看我会怎么做: http : //jsfiddle.net/jCMc8/8/

html: 的HTML:

<section>
    <div id="initRow">
        <input name="multifield" placeholder="Value">
    </div>
</section>​

javascript: javascript:

function addRow(section, initRow) {
    var newRow = initRow.clone().removeAttr('id').addClass('new').insertBefore(initRow),
        deleteRow = $('<a class="rowDelete"><img src="http://i.imgur.com/ZSoHl.png"></a>');

    newRow
        .append(deleteRow)
        .on('click', 'a.rowDelete', function() {
            removeRow(newRow);
        })
        .slideDown(300, function() {
            $(this)
                .find('input').focus();
        })
}

function removeRow(newRow) {
    newRow
        .slideUp(200, function() {
            $(this)
                .next('div:not(#initRow)')
                    .find('input').focus()
                    .end()
                .end()
                .remove();
        });
}

$(function () {
    var initRow = $('#initRow'),
        section = initRow.parent('section');

    initRow.on('focus', 'input', function() {
        addRow(section, initRow);
    });
});

​ ​

This is how I would do it. 这就是我要做的。

HTML: HTML:

<table>
    <tbody>
        <tr class="item">
            <td><input name="multifield" placeholder="Value" /></td>
            <td><i class="icon delete"></i></td>
        </tr>

        <tr class="item inactive">
            <td><input name="multifield" placeholder="Value" /></td>
            <td><i class="icon delete"></i></td>
        </tr>
    </tbody>
</table>

JavaScript: JavaScript:

$("table")
    .on("click focus", ".item.inactive", function(e) {
        var curRow = $(this);
        curRow.clone().appendTo("table tbody");
        curRow.removeClass("inactive").find("input:first").focus();
    })
    .on("click", ".icon.delete", function(e) {
        $(this).closest("tr").remove();
    });

See test case on jsFiddle . 请参阅jsFiddle上的测试用例

I have come up with a solution that seems to work so far, but I'm interested in hearing from others as to whether it is a good one. 我想出了一个到目前为止似乎可行的解决方案,但是我有兴趣听取其他人的意见,以了解这是否是一个好的解决方案。 What I do is catch the focus event for the last input field, add a new row and give focus to the input field within that row. 我要做的是捕获最后一个输入字段的焦点事件,添加新行,并将焦点赋予该行内的输入字段。 See this fiddle for working code. 有关工作代码,请参见此小提琴

HTML: HTML:

<div id="last-row">
    <input name="multifield" placeholder="Value">
</div>

JavaScript: JavaScript:

var lastRow = $('#last-row');
var lastInput = lastRow.find('input');

function addRow() {
    var newRow = $('<div><input name="multifield" placeholder="Value"><a class="row-delete"><img src="http://i.imgur.com/ZSoHl.png" /></a></div>');
    var newInput = newRow.find('input');
    lastRow.before(newRow);
    newInput.focus();

    var newDelete = newRow.find('a');
    newDelete.click(function() {
        newRow.remove();
    });
}

lastInput.focus(function() {
    addRow();
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户开始输入文本时,如何自动生成新的输入区域? - How can I automatically generate a new input area when user started to enter text? 如何在iPad上自动选择文本输入字段? - How can I automatically select a text input field on an iPad? 当用户单击按钮时,如何使其自动在文本字段中输入特定的关键字? - How can I make it automatically input particular keyword into text field when a user click a button? 当用户在输入字段上键入时,如何替换某些字符? - How can I replace certain character when a user is typing on an input field? 使用jQuery添加新输入字段时如何计算总计 - How can I calculate total when I add new input field using jQuery 如何确保在输入字段中输入新数字时脚本/<p> 标签刷新? - How can I make sure when I put a new number in input field the script/ <p> tag refreshes? 如何在字段输入中添加新列表项? - How can I add new list items on field input? 添加新行时如何自动更新起始页和结束页输入字段 - how to update a start and end page input field automatically when adding new row JavaScript在点击时生成新的输入字段 - JavaScript generate new input field on click 如何创建输入字段的动态列表,当您写入时创建另一个输入字段,当输入为空时删除它? - How can I create a dynamic list of Input field which create another input field when you write and delete it when the input is empty?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM