简体   繁体   English

onKeyUp 与一个输入字段影响另一个与 jQuery?

[英]onKeyUp with one input field affecting another with jQuery?

I've got 2 input fields.我有 2 个输入字段。 They are both currently populated.他们目前都有人口。

Input 1 = "Name"
Input 2 = "Name says this: blah blah blah"

I'm trying to figure out how to bind an onKeyUp event to both of them so that when Input 1 is changed, it will also change the value of input 2.我试图弄清楚如何将 onKeyUp 事件绑定到它们,以便在更改输入 1 时,它也会更改输入 2 的值。

For example:例如:

Input 1 = "Bla"
Input 2 = "Bla says this: blah blah blah";

I was thinking a regex, but what if it searched for "bla" on input 2, and returned 4 occurrences?我在想一个正则表达式,但是如果它在输入 2 上搜索“bla”并返回 4 次出现怎么办? That's why I was thinking of just maybe getting rid of everything after the first space in Input 2 and always replacing that value.这就是为什么我想可能只是在输入 2 中的第一个空格之后摆脱所有内容并始终替换该值。

This is what I was kind of thinking of doing:这就是我想做的事情:

        $('#input-1').bind('keyup', function() {
        $('#input-2').val().append($('#input-1'));
    });

Obviously that code doesn't work, but can anyone point me to the right direction?显然该代码不起作用,但谁能指出我正确的方向? Should I make a variable and put it in the beginning of the input box (of input 2)?我应该创建一个变量并将其放在输入框(输入 2)的开头吗?

EDIT:编辑:

This is the code that ended up working, which also doesn't delete the additional info if there is any in input-2's box:这是最终工作的代码,如果输入 2 的框中有任何附加信息,它也不会删除附加信息:

var name = $('#input-1').val();
var text = " wanted to tell you something;

$('#input-1').bind('keyup keypress blur', function() {
   name = $(this).val();
$('#share-message').val(name + text);
});

try尝试

$('#input-1').bind('keyup keypress blur', function() {
    var input2  = $('#input-2').val();
    var input1   = $(this).val();
    $('#input-2').val(input2+input1);
  });

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

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