Synchonise 2输入字段,可以使用jQuery完成吗? [英] Synchonise 2 input fields, can it be done using jQuery?

查看:42
本文介绍了Synchonise 2输入字段,可以使用jQuery完成吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当输入A被修改时,jQuery可以将一个输入字段的文本同步或复制到另一个输入字段吗?例如:

Can jQuery synchronise or copy the text of one input field to another when input A is modified? For example:

<input id="input_A" type="text" /> ...If I type something here

<input id="input_B" type="text" /> ... It will be copied here

jQuery可以这样做吗?

Can jQuery Do this?

推荐答案

试试这个:

$("#input_A").bind("keyup paste", function() {
    $("#input_B").val($(this).val());
});

对于jQuery 1.7+,在上使用

For jQuery 1.7+ use on:

$("#input_A").on("keyup paste", function() {
    $("#input_B").val($(this).val());
});

示例小提琴

2017年8月更新

Update August 2017

输入事件现在得到了很好的支持,因此您可以使用它来代替组合 keyup 粘贴事件:

The input event is now well supported, so you can use that in place of combining both keyup and paste events:

$("#input_A").on("input", function() {
  $("#input_B").val(this.value);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="input_A" type="text" />
<input id="input_B" type="text" />

这篇关于Synchonise 2输入字段,可以使用jQuery完成吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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