如何显示隐藏的输入字段

How can i show hidden input fields

我有一个问题。

当我有大约 100 个隐藏输入字段的表单时,我如何在最后一个显示的输入字段中按回车键后一一显示它们?

这会为每个 type="text" 字段添加一个密钥处理程序。键处理程序测试该字段是否是最后一个 type="text" 字段,以及该键是否是 Enter 键。如果是,它会找到下一个 type="hidden" 字段,将其类型设置为“文本”,并向其添加处理程序。

function callback(e){
    if (
        // If it's the last text field
        $(this).index() == $('#demo input[type="text"]').length - 1
            &&
        // And the key pressed is Enter:
        e.which == 13
    ) {
        // Find next hidden field, change it to text, and add the key handler:
        $(this).next('input[type="hidden"]').prop("type", "text").keypress(callback)
    }
}

// Add key handler to all text fields:
$('#demo input[type="text"]').keypress(callback)
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<div id="demo">
    <input type="text">
    <input type="text">
    <input type="text">
    <input type="hidden" value="hidden1">
    <input type="hidden" value="hidden2">
    <input type="hidden" value="hidden3">
</div>

参考文献:

jQuery: how do I check if an element is the last sibling?

How can I detect pressing Enter on the keyboard using jQuery?