繁体   English   中英

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

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

Chrome Postman扩展程序的启发,我想通过在导航到某个特定字段时自动添加新的输入字段并实现对新字段的关注,来实现多字段表单部分。 下面的屏幕截图显示了Postman中的工作原理。 最下一行包含几个输入,当导航到这些输入时,会导致在其上方添加一个新行,可以将其键入。

邮差

如何在JavaScript / jQuery中实现此行为? 为简化起见,每行只需要一个输入字段。 我创建了一个小提琴 ,应该作为解决方案的起点。

HTML示例:

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

看看我会怎么做: http : //jsfiddle.net/jCMc8/8/

的HTML:

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

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);
    });
});

这就是我要做的。

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:

$("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();
    });

请参阅jsFiddle上的测试用例

我想出了一个到目前为止似乎可行的解决方案,但是我有兴趣听取其他人的意见,以了解这是否是一个好的解决方案。 我要做的是捕获最后一个输入字段的焦点事件,添加新行,并将焦点赋予该行内的输入字段。 有关工作代码,请参见此小提琴

HTML:

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM