本文介绍了自动将字段的文本复制到另一个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要复制在字段中输入的文本(无论是键入,粘贴还是从浏览器自动填充),并在用户更改为另一个字段的同时将其粘贴到另一个字段中。
I need to copy the text entered in a field (whether it was typed in, pasted or from browser auto-filler) and paste it in another field either at the same time or as soon as the user changes to another field.
如果用户删除了field_1中的文字,它也会在字段_2中自动删除。
If the user deletes the text in field_1, it should also get automatically deleted in field_2.
我'我试过这个但它不起作用:
I've tried this but it doesn't work:
<script type="text/javascript">
$(document).ready(function () {
function onchange() {
var box1 = document.getElementById('field_1');
var box2 = document.getElementById('field_2');
box2.value = box1.value;
}
});
</script>
任何想法?
推荐答案
你差不多......函数是正确的,你只需要将它分配给输入的更改
事件:
You are almost there... The function is correct, you just have to assign it to the change
event of the input:
<script type="text/javascript">
$(document).ready(function () {
function onchange() {
//Since you have JQuery, why aren't you using it?
var box1 = $('#field_1');
var box2 = $('#field_2');
box2.val(box1.val());
}
$('#field_1').on('change', onchange);
});
这篇关于自动将字段的文本复制到另一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!