更改事件时从当前输入文本字段设置输入值 [英] Set input value from current input textfield on change event

查看:169
本文介绍了更改事件时从当前输入文本字段设置输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张带两个输入框的桌子. A和B.我在输入A(还有B)中输入内容,我希望它的值可以自动设置为在更改事件中输入的任何内容.这容易做到吗?

I have a table with two input boxes. A and B. I enter something into input A (and also B) and I want it's value to automatically be set to whatever I type into it on change event. Is this easy to do?

<tr>    
 <td><input type="text" id="A1" name="A" value=""></td>
 <td><input type="text" id="B1" name="B" value=""></td>
</tr>

推荐答案

说明

如果要在每次击键时使两个输入元素保持同步,则应使用jQuery的.keyup().change()(用于复制和粘贴)方法.

Description

If you want to keep the two input elements in sync on every keystroke you should use jQuery´s .keyup() and .change() (for copy and paste) method.

$("#A1").change(function() {
    $("#B1").val($("#A1").val());
});

$("#B1").change(function() {
    $("#A1").val($("#B1").val());
});

$("#A1").keyup(function() {
    $("#B1").val($("#A1").val());
});

$("#B1").keyup(function() {
    $("#A1").val($("#B1").val());
});

jsFiddle演示

更多信息

  • jQuery文档-.keyUp()
  • jQuery文档-.change()
  • jsFiddle Demonstration

    More Information

    • jQuery Documentation - .keyUp()
    • jQuery Documentation - .change()
    • 这篇关于更改事件时从当前输入文本字段设置输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆