简体   繁体   中英

How to empty text boxes when checkbox is unchecked using javascript

I want to empty the text boxes using Javascript when the checkbox is unchecked in HTML. Here is my code:

function sameAddress(){
    var bstreetAddress = document.getElementById("baddress").value;
    var bsuburb = document.getElementById("bsuburb").value;
    var bstate = document.getElementById("bstate").value;
    var bpostcode = document.getElementById("bpostcode").value;

    var dstreetAddress = document.getElementById("daddress");
    var dsuburb = document.getElementById("dsuburb");
    var dstate = document.getElementById("dstate");
    var dpostcode = document.getElementById("dpostcode");

    var checkBoxStatus = document.getElementById("delAddr").checked;

    if (checkBoxStatus = true){
        dstreetAddress.value = bstreetAddress;
        dsuburb.value = bsuburb;
        dstate.value = bstate;
        dpostcode.value = bpostcode;
        }
    if (checkBoxStatus = false){
        dstreetAddress.value = "";
        dsuburb.value = "";
        dstate.value = "";
        dpostcode.value = "";
    }
}

function init(){
    var checkBox = document.getElementById("delAddr");
    checkBox.onchange = sameAddress;
}

window.onload = init;

When I check the box, it is filling the same as desired, but when I uncheck the box, nothing happens at all! Where am I going wrong?

Instead of assigning true or false you must check it whether it is true or false..

if (checkBoxStatus == true){
    dstreetAddress.value = bstreetAddress;
    dsuburb.value = bsuburb;
    dstate.value = bstate;
    dpostcode.value = bpostcode;
    }
if (checkBoxStatus == false){
    dstreetAddress.value = "";
    dsuburb.value = "";
    dstate.value = "";
    dpostcode.value = "";
}

Try This

if (checkBoxStatus){
        dstreetAddress.value = bstreetAddress;
        dsuburb.value = bsuburb;
        dstate.value = bstate;
        dpostcode.value = bpostcode;
        }
    else {
        dstreetAddress.value = "";
        dsuburb.value = "";
        dstate.value = "";
        dpostcode.value = "";
    }

DEMO HERE

You are assigning it to True of false here:

if (checkBoxStatus = true){
    dstreetAddress.value = bstreetAddress;
    dsuburb.value = bsuburb;
    dstate.value = bstate;
    dpostcode.value = bpostcode;
    }
if (checkBoxStatus = false){
    dstreetAddress.value = "";
    dsuburb.value = "";
    dstate.value = "";
    dpostcode.value = "";
}

Change it to:

if (checkBoxStatus == true){
    dstreetAddress.value = bstreetAddress;
    dsuburb.value = bsuburb;
    dstate.value = bstate;
    dpostcode.value = bpostcode;
    }
if (checkBoxStatus == false){
    dstreetAddress.value = "";
    dsuburb.value = "";
    dstate.value = "";
    dpostcode.value = "";
}

if (checkBoxStatus == true){ dstreetAddress.value = bstreetAddress; dsuburb.value = bsuburb; dstate.value = bstate; dpostcode.value = bpostcode; }

Use == instead of =

In both the if conditions.

在代码中使用==而不是=它应该可以工作

Just have a look to the example on this JSFiddle .

and you can clear all the textboxes data using this simplest method.

I guess you go wrong when you use '=' , an alternative is either directly check the checkboxstatus in the if clause or use '=='

Can be done either way.

Ex :

if (checkBoxStatus == true) or if (checkBoxStatus) .. .. Followed by your code. Hope it works!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM