Javascript 如何通过单击复选框将文本元素从输入复制到另一个输入?

Javascript 如何通过单击复选框将文本元素从输入复制到另一个输入?,javascript,Javascript,我是个新手。如果单击复选框,我想将输入文本从shippingName和shippingZip复制到输入文本billingName和billingZip。我试过好几件事,但都没有结果 这是我的密码: function billingFunction(){ var shippingName=document.getElementById(“shippingName”); var shippingZip=document.getElementById(“shippingZip”); var bill

我是个新手。如果单击复选框,我想将输入文本从shippingName和shippingZip复制到输入文本billingName和billingZip。我试过好几件事,但都没有结果

这是我的密码:

function billingFunction(){
var shippingName=document.getElementById(“shippingName”);
var shippingZip=document.getElementById(“shippingZip”);
var billingName=document.getElementById(“billingName”);
var billingZip=document.getElementById(“billingZip”);
if(document.getElementById(“相同”).checked==true){
document.getElementById(“billingName”).value=document.getElementById(“billingName”).value;
document.getElementById(“billingZip”).value=document.getElementById(“billingZip”).value;
}
}

装运信息
姓名:

邮政编码:
账单信息是否相同? 计费信息 姓名:
邮政编码:

因为您正在将值从
BillingName
分配给
BillingName
billingZip
分配给
billingZip
,该值为空

下面是一个JavaScript:

function billingFunction() {

  var shippingName = document.getElementById("shippingName");
  var shippingZip = document.getElementById("shippingZip");
  var billingName = document.getElementById("billingName");
  var billingZip = document.getElementById("billingZip");
  if (document.getElementById("same").checked == true) {
    billingName.value = shippingName.value;
    billingZip.value = shippingZip.value;
  }
}
2件事: 不要将布尔变量与布尔基元进行比较:

if(document.getElementById("same").checked == true) //DON'T NEED THIS
只要做:

if(document.getElementById("same").checked)
您重复了
billingZip
billingName
。在这里:

document.getElementById("billingName").value = document.getElementById("billingName").value;
document.getElementById("billingZip").value = document.getElementById("billingZip").value;
function billingFunction(){
var shippingName=document.getElementById(“shippingName”);
var shippingZip=document.getElementById(“shippingZip”);
var billingName=document.getElementById(“billingName”);
var billingZip=document.getElementById(“billingZip”);
if(document.getElementById(“相同”).checked){
document.getElementById(“billingName”).value=document.getElementById(“shippingName”).value;
document.getElementById(“billingZip”).value=document.getElementById(“shippingZip”).value;
}
}

装运信息
姓名:

邮政编码:
账单信息是否相同? 计费信息 姓名:
邮政编码: