如果选中此复选框,我必须复制发货名称和邮政编码,然后在帐单名称和邮政编码中复制;如果未选中此复选框,则将帐单名称和zip保留为空。
以下是我的JavaScript代码:
function billingFunction(){
if(document.getElementById('same').checked==true){
document.getElementById("billingName").value = document.getElementById("shippingName").value;
document.getElementById("billingZip").value = document.getElementById("shippingZip").value;
}
else{
document.getElementById("billingZip").value = '';
document.getElementById("billingName").value = '';
}
}
same
是复选框的ID。
此代码有什么问题?我不能使用jQuery,而必须使用id
属性来访问HTML。
您的javascript看起来不错,因此让我假设问题出在如何触发billingFunction()
函数上。
我建议做这样的事情:
//use your same billingFunction
function billingFunction(){
...
}
/*
Once the document is ready, attach an event listener to the checkbox
which fires the function when the checkbox is changed
*/
window.onload = function() {
document.getElementById('same').addEventListener('change', billingFunction);
}
这样可以使页面恢复正常,然后再将eventListener附加到您的复选框。复选框值更改(选中/未选中)时,将触发billingFunction。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句