如果使用JavaScript选中按钮,则将发货复制到账单地址

如果使用JavaScript选中按钮,则将发货复制到账单地址,javascript,html,Javascript,Html,当我们单击复选框时,我试图将发货地址复制到账单中,但它不起作用 这是HTML <h1>JavaScript Homework</h1> <p>Add the JavaScript code needed to enable auto-complete on this form. Whenever the checkbox is checked, the code should automatically copy the values from Sh

当我们单击复选框时,我试图将发货地址复制到账单中,但它不起作用

这是HTML

<h1>JavaScript Homework</h1>
    <p>Add the JavaScript code needed to enable auto-complete on this form.  Whenever the checkbox is checked, the code should automatically copy the values from Shipping Name and Shipping Zip into the Billing Name and Billing Zip.  If the checkbox is unchecked, the Billing Name and Billing Zip should go blank.</p>

<form>
    <fieldset>
        <legend>Shipping Information</legend>
        <label for ="shippingName">Name:</label>
        <input type = "text" name = "shipName" id = "shippingName" required><br/>
        <label for = "shippingZip">Zip code:</label>
        <input type = "text" name = "shipZip" id = "shippingZip" pattern = "[0-9]{5}" required><br/>
    </fieldset>
    <input type="checkbox" id="same" name="same" onchange= "billingFunction()"/>
    <label for = "same">Is the Billing Information the Same?</label>

    <fieldset> 
        <legend>Billing Information</legend>
        <label for ="billingName">Name:</label>
        <input type = "text" name = "billName" id = "billingName" required><br/>
        <label for = "billingZip">Zip code:</label>
        <input type = "text" name = "billZip" id = "billingZip" pattern = "[0-9]{5}" required><br/>
    </fieldset>
        <input type = "submit" value = "Verify"/>
    </form>

我不知道为什么它不起作用。谢谢您的帮助。

不要在不使用变量的情况下将值设置为变量。 这将有助于:

var SN = document.getElementById("shippingName");
var SZ = document.getElementById("shippingZip");
var BN = document.getElementById("billingName");
var BZ = document.getElementById("billingZip");

function billingFunction() {
  if (document.getElementById("same").checked == true) {
    BN.value = SN.value;
    BZ.value = SZ.value;
  } else {
    BN.value = "";
    BZ.value = "";
  }
}

定义“它不起作用”。你可能想看看关于如何提问的常见问题。线索#1:你得到了值,然后什么也不做。然后设置为立即丢弃的局部变量。在web上搜索如何设置表单字段值。谢谢,我已经解决了这个问题。很高兴听到这个问题。谢谢,这很有帮助,我需要添加{BN.value=“”;BZ.value=“”;}以使else也能工作。
var SN = document.getElementById("shippingName");
var SZ = document.getElementById("shippingZip");
var BN = document.getElementById("billingName");
var BZ = document.getElementById("billingZip");

function billingFunction() {
  if (document.getElementById("same").checked == true) {
    BN.value = SN.value;
    BZ.value = SZ.value;
  } else {
    BN.value = "";
    BZ.value = "";
  }
}