簡體   English   中英

復選框不會使用 Javascript 檢入 IE7,但沒有錯誤

[英]Checkboxes will not check in IE7 using Javascript, and yet no errors

好吧,我對這個完全困惑。

我有一個腳本,它從 JSON 對象接收一堆值並創建一堆復選框,然后根據它們的值選中或取消選中這些復選框。

該腳本在 IE8、Firefox3 等中正常工作......等等......

然而...

在 IE7 中,腳本無法勾選復選框。 它沒有顯示任何錯誤,據我所知,腳本運行得很好。 我只是沒有選中任何復選框,我不知道為什么......

shoppingCart['Update_Stock_Item_0_NRD%5FHAT2'] = {
  'propeller': {
    'label': 'propeller',
    'optionValues': {
      'on': {
        'selected': 'selected'
      },
      'off': {
        'selected': ''
      },
      '': new String()
    }
  },
  'sunLogo': {
    'label': 'sunLogo',
    'optionValues': {
      'on': {
        'selected': 'selected'
      },
      'off': {
        'selected': ''
      },
      '': new String()
    }
  },
  'MSLogo': {
    'label': 'sunLogo',
    'optionValues': {
      'on': {
        'selected': 'selected'
      },
      'off': {
        'selected': ''
      },
      '': new String()
    }
  }
};

function stockInit() {
  alert("BEGIN: stockInit()");
  // TODO: You will recieve an "on" and an "off" option, 
  //       One will have a "selected" attribute of "selected",
  //       and the other will have a "selected" attribute of ""
  //
  //         The option that has the "selected" attribute of "" 
  //         will generate a checkbox that is not checked.
  //
  //         The option that has the "selected attribute of "selected"
  //       will generate a checkbox that is checked.
  //  
  //         Why? You ask...because that's just the way the thing is 
  //                        setup.
  for (var item in shoppingCart) {
    // // console.log("processing item: " + item);

    var optionContainer = document.getElementById(item + "_optionContainer");

    for (var option in shoppingCart[item]) {
      if (option != "blank") {
        // // console.log("option: " + option);

        var currentOption = shoppingCart[item][option]['optionValues'];

        // // console.log("currentOption['on']['selected']: " + currentOption['on']['selected']);
        // // console.log("currentOption['off']['selected']: " + currentOption['off']['selected']);

        // Really you only have to check the one, but just to be through-o
        var selected = (currentOption['on']['selected'] == 'selected') ? true : false;
        selected = (currentOption['off']['selected'] == 'selected') ? false : true;

        var label = document.createElement("LABEL");
        var labelText = document.createTextNode(shoppingCart[item][option]['label']);
        var optionInput = document.createElement("INPUT");

        var hiddenInput = document.createElement("INPUT");

        optionInput.setAttribute("type", "checkbox");
        optionInput.checked = selected;

        optionInput.setAttribute("id", option);
        alert(optionInput.id);
        alert(optionInput.checked);

        hiddenInput.setAttribute("type", "hidden");
        hiddenInput.setAttribute("name", option);
        hiddenInput.setAttribute("id", option + "_hiddenValue");
        hiddenInput.setAttribute("value", (optionInput.checked) ? "on" : "off");

        label.appendChild(optionInput);
        label.appendChild(labelText);
        label.appendChild(hiddenInput);

        (function(id) {
          optionInput.onclick = function() {
            var hiddenInput = document.getElementById(id + "_hiddenValue");

            hiddenInput.setAttribute("value", (this.checked == true) ? "on" : "off");
            alert("this.id: " + this.id);
            alert("this.checked: " + this.checked);
          }
        })(optionInput.id);

        optionContainer.appendChild(label);
      }
    }
    // // console.log("processing item of " + item + " complete");
  }
  alert("END: stockInit()");
}

並且請不要問我為什么要這樣做......我只能告訴你我無權訪問后端代碼......所以我得到了我得到的......

我想這是你的問題

基本上解決方案是另外這樣做:

optionInput.defaultChecked = selected;

或者在將復選框插入 DOM 后設置選中的參數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM