2016-10-14 141 views
0

Html - 傳遞地址必須將輸入的值賦予帳單地址。在JavaScript和HTML在不同的文件當單擊複選框時,使用複選框將字段中輸入的值顯示到另一個字段

其他JS功能都工作正常,但它只是這一次似乎沒有工作

<div class="textinput"> 
    <label for="deladdress">Delivery Address: </label> 
    <input id="deladdress" type="text" name="Delivery Address" /> 
</div> 
<div id="buttoncheckarea"> 
    <input id="CheckDelivery" type="checkbox" name="CheckDelivery"> 
    <label id="diff" for="CheckDelivery">Same as delivery address?</label> 
</div> 
<div class="textinput"> 
    <label for="postcode">Postcode: </label> 
    <input id="postcode" type="text" name="postcode" /> 
</div> 
<div class="textinput"> 
    <label for="billaddress">Billing Address: </label> 
    <input id="billaddress" type="text" name="Billing Address" /> 
</div> 

的JavaScript

function deliveryfunc() { 
    var delivery = document.getElementById("deladdress").value; 
    var billing = document.getElementById("billaddress").value; 
    //var checkbox = document.getElementById("CheckDelivery").checked; 
    if (document.getElementsByName("CheckDelivery").checked == true) {  
     billing = delivery; 
    } 
} 

function init() { 
    var order = document.getElementById("ordForm"); 
    order.onsubmit = ordervalidation; 
    order.onclick = radiobuttoncheck; 
    var checkbutton = document.getElementsByName("CheckDelivery"); 
    checkbutton.onclick = deliveryfunc; 
} 

window.onload = init; 

回答

0

嘗試更新deliveryfunc如下:

function deliveryfunc() { 
    var delivery = document.getElementById("deladdress"); 
    var billing = document.getElementById("billaddress"); 
    if (document.getElementById("CheckDelivery").checked == true) {  
     billing.value = delivery.value; 
    } 
} 

function init() { 
    var order = document.getElementById("ordForm"); 
    order.onsubmit = ordervalidation; 
    order.onclick = radiobuttoncheck; 
    var checkbutton = document.getElementById("CheckDelivery"); 
    checkbutton.onclick = deliveryfunc; 
} 

window.onload = init; 
+0

我試過了,它仍然不適合我。 – Nate

+0

找到了問題,你正在使用'getElementsByName',它返回一個集合[getElementsByName](http://www.w3schools.com/jsref/met_doc_getelementsbyname.asp)。通過將其更改爲'getElementByName',代碼將起作用,更新答案。 – mergenchik

+0

也可以使用'document.getElementsByName(「CheckDelivery」)[0]'這將返回名爲CheckDelivery的第一個元素 – mergenchik

相關問題