无法替换复选框Javascript

时间:2017-08-01 01:53:03

标签: javascript

我正在为网页创建一个mozilla添加。使用附加的Javascript代码,我可以创建复选框。该复选框应替换为其他复选框,具体取决于下拉列表中的选定值。 Onclick on dropdown我正在调用fun_sub_cat函数,它正在创建复选框并且还替换但它只替换一次。例如:如果下拉列表1和2中有2个选项,则选择选项1复选框值应为1,2,3,对于选项2复选框值应为a,b,c。

在点击功能上它创建复选框并且第一次更改值也会替换复选框值,但是当我在下拉列表上单击第3次或更改值时,它不起作用。它获得第二次更改的复选框值。

var k = 0;
    function fun_cat_sub() {
    console.log("Value Changed");
    var Myarry = "";
        if(document.getElementById('sub_cat').value == 'Password Reset'){
            Myarry = check_list1;
              if(k == 0){
                console.log("creating checkbox for the first time");
                k = k+1;
                for (var i = 0; i < Myarry.length; i++) {
                    var checkbox = document.createElement('input');
                    checkbox.type = "checkbox";
                    checkbox.id = "checkBox"+i;
                    checkbox.value = Myarry[i];
                    checkbox.class = "_class";
                    checkbox.appendAfter(br_3);
                    var lab_chk = document.createElement('label');
                    lab_chk.innerHTML = Myarry[i];
                    lab_chk.id = "lab_chk"+i;
                    lab_chk.value = Myarry[i];
                    lab_chk.style.marginLeft = "10px";
                    lab_chk.appendAfter(checkbox);
                    document.createElement('br').appendAfter(lab_chk);
                }
              }
            else if(k != 0){
              console.log("I am in 1st Loop");
              checking(Myarry);
            }
          }
        else {
            Myarry = check_list2;
            console.log("I am in 2nd Loop");
            checking(Myarry);
        }
    }

    function checking(Myarry){
      console.log(Myarry.length);
      for (var i = 0; i < Myarry.length; i++) {
        var old_chk = document.getElementById('checkBox'+i);
        var new_chk = document.createElement("input");
        new_chk.type = "checkbox";
        new_chk.id = "checkBox"+i;
        new_chk.value = Myarry[i];
        old_chk.parentNode.replaceChild(new_chk, old_chk);
        var old_lab_chk = document.getElementById('lab_chk'+i);
        var new_lab_chk = document.createElement('label');
        new_lab_chk.innerHTML = Myarry[i];
        new_lab_chk.style.marginLeft = "10px";
        old_lab_chk.parentNode.replaceChild(new_lab_chk, old_lab_chk);
    }
    }

0 个答案:

没有答案