Javascript Internet Explorer问题 - 我做错了什么?

时间:2012-02-10 01:30:27

标签: javascript internet-explorer

我看过很多帖子都无济于事。我有一个简单的表格,其中一个产品根据检查的复选框数量而变化。它适用于IE以外的所有浏览器。我究竟做错了什么?

<body>
<script type="text/javascript">
function check(){
"use strict";
var count = 0, x=0, checkboxes=document.signup.getElementsByClassName("styled");
for(;x<checkboxes.length; x++){
    if(checkboxes[x].checked){
        count++;
    }
}
if(count<3) {
   document.getElementById("variable").value = "1";
}
else if (count == 3){

    document.getElementById("variable").value = "74";
}
else if (count == 4){

    document.getElementById("variable").value = "75";
}
else if (count == 5){

    document.getElementById("variable").value = "76";
}

}
</script>
<form name="signup" id="signup" method="post" action="/subscribers/signup.php">
<input type="checkbox" id="variable" name="product_id[]" value="" class="styled"></input>product 1 - variable</div>
<input type="checkbox" id="same" name="product_id[]" value="3" class="styled"></input>product 2
<input type="checkbox" id="same2" name="product_id[]" value="2" class="styled"></input>product 3
<input type="checkbox" id="same3" name="product_id[]" value="4" class="styled"></input><div class="check-title">product 4
<input type="checkbox" id="same4" name="product_id[]" value="44" class="styled"></input><div class="check-title">product 5          
<a href="#" id="submit" onClick="check()">Continue</a></td></tr>
</form>
</body>

2 个答案:

答案 0 :(得分:0)

IE9之前的所有IE版本都不支持getElementsByClassName()。你需要使用某种替代品。

而不是你的代码:

checkboxes = document.signup.getElementsByClassName("styled");

我建议使用这个:

checkboxes = document.getElementById("signup").getElementsByTagName("input")

getElementsByTagName()在IE的所有版本中得到广泛支持。这显然会获得所有input个标记,但只有复选框会设置checked,所以您应该没问题。

如果你需要按类筛选,那么你可以这样做:

function check() {
    "use strict";

    // initialize checkbox count to 0
    var count = 0, item;

    // get all input tags in the form
    var inputs = document.getElementById("signup").getElementsByTagName("input");

    // loop through all input tags in the form
    for (var i = 0; i < inputs.length; i++) {
        // get this one into the local variable item
        item = inputs[i];
        // if this input tag has the right classname and is checked, increment the count
        if ((item.className.indexOf("styled") != -1) && item.checked) {
            count++;
        }
    }
    // get object for result
    var obj = document.getElementById("variable");
    // check count and set result based on the count
    if(count < 3) {
        obj.value = "1";
    } else if (count == 3) {
        obj.value = "74";
    } else if (count == 4) {
        obj.value = "75";
    } else if (count == 5) {
        obj.value = "76";
    }
}

答案 1 :(得分:-1)

IE没有方法getElementsByClassName ...您可以尝试定义它:

if(document.getElementsByClassName == undefined) { 
   document.getElementsByClassName = function(cl) { 
      var retnode = []; 
      var myclass = new RegExp('\\b'+cl+'\\b'); 
      var elem = this.getElementsByTagName('*'); 
      for (var i = 0; i < elem.length; i++) { 
         var classes = elem[i].className; 
         if (myclass.test(classes)) { 
            retnode.push(elem[i]); 
         } 
      } 
      return retnode; 
   } 
};