javascript - 检测输入变化并同时写入其他输入

标签 javascript jquery html

我有两个输入文本框。我想如果第一个输入发生变化并同时写入其他输入。

我使用this代码。我尝试这样做,但在打开第一页时不起作用。如果页面刷新一次,则此代码有效。我写了一些带有一些选项的东西。 我的代码是 here

$.event.special.inputchange = {
    setup: function() {
        var self = this, val;
        $.data(this, 'timer', window.setInterval(function() {
            val = self.value;
            if ( $.data( self, 'cache') != val ) {
                $.data( self, 'cache', val );
                $( self ).trigger( 'inputchange' );
            }
        }, 20));
    },
    teardown: function() {
        window.clearInterval( $.data(this, 'timer') );
    },
    add: function() {
        $.data(this, 'cache', this.value);
    }
};

$('input').on('inputchange', function() {
    var container = $("div.class1").first();
     $('#firstname').on('inputchange', function() {
        container.find("input.firstname").val(this.value);
    });
});

步骤:

  1. 有什么变化
  2. 找到类(class)中的第一个 child
  3. 一流的值(value)就是平等的变化

我能做什么?

最佳答案

<强> Fiddle Demo

对于第一个 child ,请使用input:first。 - <强> :first

试试这个片段,

$('input#firstname').on('inputchange', function() {
    var container = $("div.class1").first();

    container.find("input:first").val(this.value);

});

关于javascript - 检测输入变化并同时写入其他输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24361437/

相关文章:

javascript - PowerBI Embedded 垂直滚动条不可见

html - 如何在 html 中创建可变大小的 tile 布局

html - css:50% 宽度 div 之间的 20px 间距

javascript - getInitialProps 在 Next.js 中导入数据

javascript - 在容器内拖动图像,不使用鼠标移动,稍微快一点

javascript - 风 sails JS : How to return a value from a service

javascript - 如何获取包含JavaScript脚本 block 的div内容?

生成可编辑 "tags"的javascript库/jquery插件

jQuery sibling 标签 "a"

html - css级联问题:Mozilla