单击复选框时,使用复选框将字段中输入的值显示到另一个字段

时间:2016-10-14 10:30:49

标签: javascript html

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;

1 个答案:

答案 0 :(得分: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;