5

我需要一个 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

4 回答 4

13

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

于 2012-07-26T05:16:10.473 回答
0

通过使用upper 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;
        
      }
于 2021-01-20T05:15:02.670 回答
0

这就是我取消选中的方法:

    document.getElementById("Name2").value ="";
    document.getElementById("Name3").value="";  
于 2020-05-25T06:41:52.420 回答
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>

于 2020-06-16T07:19:32.500 回答