动态输入字段的实时焦点问题

发布于 2025-01-06 05:39:06 字数 828 浏览 4 评论 0原文

我正在尝试制作一个动态输入字段解决方案,类似于我的 动态选择行方法。

这个想法很简单。总会有一个额外的字段。因此,当最后一个字段获得焦点时,它将克隆它并将其附加到包装器中。

我当前的代码适用于.focus() 等不是动态的。但是, .live('focus', function () {}) 不会。此外,“实时改变”或“实时聚焦”也不起作用。

这看起来很奇怪的问题,因为它在 这个问题,从1.4版本开始应该支持“实时对焦”。但它显然不起作用。大多数其他问题都有非常本地化的解决方案,具有替代功能。

这就是我的知识的终点,我没有更多的想象力来寻找替代解决方案。

PS:如果这个想法完全失败,那么更改事件触发器将是一个替代方案。但是,“实时更改”不是仅适用于选择和/或勾选输入吗?

I'm trying to make a dynamic input fields solution, similar to my dynamical select rows method.

The idea is simple. There always will be a extra field. So when the last field is in focus, it will clone it and append it to the wrapper.

My current code works with .focus(), and so, is not dynamic. However, .live('focus', function () {}) doesn't. Also, "live change" or "live focusin" doesn't work.

This seems very weird problem, as it is cleared out in this question, that "live focus" should be supported since the version 1.4. However it clearly doesn't work. And most of these other questions have very localized solutions, with alternative functions.

And this is where my knowledge ends and I don't have any more imagination, for an alternative solution.

PS: If this idea is a total bust, then a change event trigger would be an alternative. However, isn't "live change" for select and/or tick inputs only?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满地尘埃落定 2025-01-13 05:39:06

无法说出为什么 .live 使您失败(示例失败的代码会很好),但为什么不在克隆期间添加对 .focus 的调用?将新字段添加到 DOM 后,这将是一件微不足道的事情。不确定“动态”在这种情况下意味着什么,但听起来这就是您所需要的。过去使用这种技术实现了类似的表单元素,效果很好。

Can't speak to why .live is failing you (example failing code would be nice), but why don't you add the call to .focus during your cloning? Would be trivial to do just after adding the new field to the DOM. Not sure what "dynamic" means in this context, but sure sounds like that's all you need. Have implemented similar form elements in the past using this technique, works great.

谁的年少不轻狂 2025-01-13 05:39:06

所以毕竟,问题不直接在于实时对焦功能,而在于获取正确的最后一个字段。该代码不够动态,无法真正重新计算当前的最后一个。虽然我的某些部分认为 .live() 会处理所有事情,但事实并非如此(不要在累的时候编码!)。

因此,当我将实时函数选择器手动更改为 .dynamicalfields_wrapper input:last 时,它工作得很好。所以我修改了我的代码以使其更加动态,但仍然使用全局 jQuery 的包装器选择器功能。

(function ($) {
    $.fn.DynamicalFieldsUpdater = function () {
        return this.each(function (i, wrapper) {
            $(wrapper).find('input:last').css('background', 'red').live('focus', function (i, last) {
                $(wrapper).append($(this).clone().val(''));
            });
        });
    };
})(jQuery);
$('.dynamicalfields_wrapper').DynamicalFieldsUpdater();

[查看输出]

按预期工作。也适用于更改,只是不是“更改”更改,而是按键

So after all, the problem wasn't directly in the live focus function, but rather getting the correct last field. The code wasn't dynamic enough, to really re-calculate what is the currently last one. And some part of me though, that .live() takes care of everything, but it doesn't (Don't code while tired!).

So when I changed the live functions selector manually to .dynamicalfields_wrapper input:last, it worked fine.. So I modified my code to be more dynamic, but yet use the wrappers selector that comes to the global jQuery function.

(function ($) {
    $.fn.DynamicalFieldsUpdater = function () {
        return this.each(function (i, wrapper) {
            $(wrapper).find('input:last').css('background', 'red').live('focus', function (i, last) {
                $(wrapper).append($(this).clone().val(''));
            });
        });
    };
})(jQuery);
$('.dynamicalfields_wrapper').DynamicalFieldsUpdater();

[View output]

Works like desired. Also works with change, only that not "change"-change, but keypress.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文