如何在使用javascript取消选中复选框时清空文本框

时间:2014-04-21 07:00:16

标签: javascript html

我想在HTML中取消选中复选框时使用Javascript清空文本框。这是我的代码:

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;

当我选中此框时,它会根据需要填充相同的内容,但是当我取消选中该框时,根本没有任何反应!我哪里错了?

7 个答案:

答案 0 :(得分:1)

不是分配真或假,你必须检查它是真还是假..

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 = "";
}

答案 1 :(得分:1)

试试这个

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

答案 2 :(得分:0)

您在此处将其指定为True:

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;
    }
if (checkBoxStatus == false){
    dstreetAddress.value = "";
    dsuburb.value = "";
    dstate.value = "";
    dpostcode.value = "";
}

答案 3 :(得分:0)

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

使用==而不是=

在两种if条件下。

答案 4 :(得分:0)

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

答案 5 :(得分:0)

只需查看example on this JSFiddle

您可以使用这种最简单的方法清除所有文本框数据。

答案 6 :(得分:0)

我猜你使用&#39; =&#39; ,另一种方法是直接检查if子句中的checkboxstatus或使用&#39; ==&#39;

可以两种方式完成。

前:

if(checkBoxStatus == true)或if(checkBoxStatus) .. .. 跟着你的代码。 希望它有效!!