Javascript 将值从一个输入字段复制到另一个输入字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11662715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copying value from one input field to another input field
提问by steeve
I need a javascript function for copying the value of one input field to another input field based on checkbox selection. However i did some javascript code on click,
我需要一个 javascript 函数,用于根据复选框选择将一个输入字段的值复制到另一个输入字段。但是我点击了一些javascript代码,
<script>
function copyTextValue() {
var text1 = document.getElementById("Name1").value;
document.getElementById("Name2").value = text1;
document.getElementById("Name3").value=text1;
}
</script>
<input type="checkbox" name="check1" onclick="copyTextValue();"/>
Now i need to delete the copied values in those two boxes on uncheck. I stuck up with this. Any help?
现在我需要在取消选中这两个框中删除复制的值。我坚持了这一点。有什么帮助吗?
回答by bugwheels94
function copyTextValue(bf) {
var text1 = bf.checked ? document.getElementById("Name1").value : '';
document.getElementById("Name2").value = text1;
document.getElementById("Name3").value = text1;
}
<input type="checkbox" name="check1" onchange="copyTextValue(this);" />
<input id="Name1"><input id="Name2"><input id="Name3">
回答by Pournami K K
This is how I do for unchecking:
这是我取消选中的方式:
document.getElementById("Name2").value ="";
document.getElementById("Name3").value="";