将值从一个输入字段复制到另一个输入字段

时间:2012-07-26 05:11:09

标签: javascript html

我需要一个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();"/>

现在我需要在取消选中时删除这两个框中的复制值。我坚持这个。有什么帮助吗?

4 个答案:

答案 0 :(得分:9)

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">

答案 1 :(得分:0)

这是我取消选中的方式:

    document.getElementById("Name2").value ="";
    document.getElementById("Name3").value="";  

答案 2 :(得分:0)

function copyTextValue() {

    if(document.getElementById('check1').checked){
        let text1 = document.getElementById('Name1').value;        
        document.getElementById('Name2').value = text1;
        document.getElementById('Name3').value = text1;
    }
    else{
        document.getElementById('Name2').value = "";
        document.getElementById('Name3').value = "";
    }    
}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
  <input type="text" id="Name1" name="Name1">
  <input type="text" id="Name2" name="Name2">
  <input type="text" id="Name3" name="Name3">  
  
  <input type="checkbox" id="check1" name="check1" onclick="copyTextValue();"/>

</body>
</html>

答案 3 :(得分:0)

通过使用上层 ans,我将帐单地址复制到 woocommerce 中的送货地址

 function copyTextValue(bf) {
        var billing_first_name = bf.checked ? document.getElementById("billing_first_name").value : '';
        var billing_last_name = bf.checked ? document.getElementById("billing_last_name").value : '';
        var billing_address_1 = bf.checked ? document.getElementById("billing_address_1").value : '';
        var billing_address_2 = bf.checked ? document.getElementById("billing_address_2").value : '';
        var billing_city = bf.checked ? document.getElementById("billing_city").value : '';
        var billing_state = bf.checked ? document.getElementById("billing_state").value : '';
        var billing_postcode = bf.checked ? document.getElementById("billing_postcode").value : '';
        document.getElementById("shipping_first_name").value = billing_first_name;
        document.getElementById("shipping_last_name").value = billing_last_name;
        document.getElementById("shipping_address_1").value = billing_address_1;
        document.getElementById("shipping_address_2").value = billing_address_2;
        document.getElementById("shipping_city").value = billing_city;
        document.getElementById("shipping_state").value = billing_state;
        document.getElementById("shipping_postcode").value = billing_postcode;
        
      }