2011-08-03 46 views
0

此错误仅在Internet Explorer中存在,但在目前为止可用于Mozilla。基本上,当某人在下拉菜单中选择一个adtype时,它应该会显示相应的下拉菜单。我会发布相关的代码。Internet Explorer中的Javascript错误

<script language="javascript"> 
    /* Hide and show appropriate drop down menu */ 
    var subs_array = new Array("pricefreq","pricetype"); // The id's of the hidden divs 
    function displayOptions(the_sub){ 
     for (i=0;i<subs_array.length;i++){ 
      var my_sub = document.getElementById(subs_array[i]); 
      my_sub.style.display = "none"; 
     } 
     document.getElementById(the_sub).style.display = "inline-block"; 
    } 


    </script> 

       <select name="adtype" id="adtype" tabindex="1"> 
        <option value="unselected">Please Select...</option> 
        <option value="to_buy" onmousedown="displayOptions('pricetype')">For Sale</option> 
        <option value="to_rent" onmousedown="displayOptions('pricefreq')">For Rent</option> 
       </select> 

       <select name="pricetype" id="pricetype" tabindex="8"> 
        <option value="unselected">Please Select...</option> 
        <option value="Starting at">Starting At</option> 
        <option value="Fixed Price">Fixed Price</option> 
        <option value="Offers over">Offers Over</option> 
       </select> 

       <select name="pricefreq" id="pricefreq" tabindex="9"> 
        <option value="unselected">Please Select...</option> 
        <option value="Per Week">Per Week</option> 
        <option value="Per Month">Per Month</option> 
        <option value="4 Weekly">4 Weekly</option> 
       </select> 
+3

什么是错误? –

+0

我说错了,我的意思是说它没有做它打算做的事情。所以基本上它什么都不做 – user29660

回答

0

我不认为鼠标事件在IE中的选项标签上工作。您可以将onchange事件应用于select元素并提供值onchange="displayOptions(this.value)",然后转换JS中的值,或者将该选项的值更改为应显示的ID。

例子:http://jsfiddle.net/dxhBs/1/

+0

这工作,佩曼斯答复没有。感谢 – user29660

+0

只是一个供参考,您可能想要将这些额外的选择组设置为无。 – vol7ron

+0

我为什么要这么做? – user29660

0

你可以重构你的代码如下

var subs_array = new Array("pricefreq","pricetype"); // The id's of the hidden divs 
function displayOptions(val){  
    if(val == 'unselected') 
     return; 
    for (i=0;i<subs_array.length;i++){ 
     var my_sub = document.getElementById(subs_array[i]); 
     my_sub.style.display = "none"; 
    } 

    document.getElementById(val).style.display = "inline-block"; 
} 

  <select name="adtype" id="adtype" tabindex="1" onchange="displayOptions(this.value);"> 
       <option value="unselected">Please Select...</option> 
       <option value="pricetype" >For Sale</option> 
       <option value="pricefreq" >For Rent</option> 
      </select> 

      <select name="pricetype" id="pricetype" tabindex="8"> 
       <option value="unselected">Please Select...</option> 
       <option value="Starting at">Starting At</option> 
       <option value="Fixed Price">Fixed Price</option> 
       <option value="Offers over">Offers Over</option> 
      </select> 

      <select name="pricefreq" id="pricefreq" tabindex="9"> 
       <option value="unselected">Please Select...</option> 
       <option value="Per Week">Per Week</option> 
       <option value="Per Month">Per Month</option> 
       <option value="4 Weekly">4 Weekly</option> 
      </select> 
+0

这是我描述的解决方案之一。 – vol7ron

0

上的选项标签中删除您onmousedown和选择标签创建onchange

<select name="adtype" id="adtype" tabindex="1" onchange="displayOptions(this.value)"> 
    <option value="unselected">Please Select...</option> 
    <option value="to_buy">For Sale</option> 
    <option value="to_rent">For Rent</option> 
</select> 

然后这里是脚本:

<script type="text/javascript"> 
    /* Hide and show appropriate drop down menu */ 
    var subs = {"to_buy" : "pricetype", "to_rent" : "pricefreq"}; // The id's of the hidden divs 
    function displayOptions(value) { 
     for(sub in subs) { 
      var my_sub = document.getElementById(subs[sub]); 
      if(value == sub) my_sub.style.display = "inline-block"; 
      else my_sub.style.display = "none"; 
     } 
    } 
</script>