2009-06-26 22 views
1

好吧,我完全困惑在這一個。複選框將不會檢查IE7使用Javascript,但沒有錯誤

我有一個腳本從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() 
               } 
         }       
}; 

功能stockInit(){ 警報( 「BEGIN:stockInit()」); // TODO:你會收到一個「開」和「關」的選項, //一個將有一個「selected」屬性爲「selected」, //另一個將有一個「selected」屬性爲「 「 // //」選擇「屬性爲」「 //的選項將生成未選中的複選框。 // //選中「選中屬性」選項「 //」的選項將會生成一個已選中的複選框 //
//爲什麼?您問...因爲這就是事物的方式是 //設置 爲(在購物車VAR項目) { // //執行console.log( 「處理項目:」 +項目);

 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()"); 
} 

請不要問我爲什麼」以這種方式做事...我真的可以告訴你的是,我沒有訪問後端代碼...所以我得到我得到的...

+0

後,你有沒有嘗試過設置「optionInput .checked = true;「看看是否有任何改變? – 2009-06-26 14:30:31

+0

提出一個新的標籤:空白 - 瘋狂。嚴重的是,嘗試使用少於113個空格的縮進。 – phihag 2009-06-26 14:40:02

回答

13

我猜this is your problem

基本的解決方案是額外做到這一點:

optionInput.defaultChecked = selected; 

或備選地,設置了檢查參數插入複選框到DOM