2011-02-07 127 views
0

我遇到Chrome问题。我有一个下拉列表。除Chrome之外,它运行良好。然而,使用chrome,它首先会添加一个空元素。Chrome下拉列表问题

HTML部分:

<head> 
<script type="text/javascript">  
    $(document).ready(function() { 
    townDistrictObject.bind({cityId:1}); 
}); 
</script> 
</head> 
    <body>    
     <div class="left marginleft15"> 
      <p>Town</p> 
      <p> 
       <select name="town1" id="townId" style="width: 152px;"> 
        <option selected="selected" value="999999999">Whole Istanbul</option> 
        <option value="999999998">Anatolian Part</option> 
       </select> 
      </p> 
     </div> 
     <div class="left marginleft15"> 
      <p>District</p> 
      <p> 
       <select id="districtId" name="districtid1" style="width: 174px;" > 
        <option selected="selected" value="0">Whole Districts</option> 
       </select> 
      </p> 
     </div> 
    </body> 

有类似的东西在脚本的一面:

var townDistrictObject = { 
    defaults : { 
     cityId :1, 
     townElementId : "townId", 
     districtElementId : "districtId", 
     allDistricts: true 
}, 

bind : function(options) { 
    $.extend(this.defaults, options); 
    var that = this; 
    var opts = this.defaults; 
    var townElement = $('#' + opts.townElementId); 
    townElement.val(0);   
} 
}; 

的问题说明:

首先,有一个空元素在列表顶部uldn't是!)

enter image description here

其次,我会点击其中的一个。( “全伊斯坦布尔” 我的例子。)

enter image description here

最后,我检查列表和顶部的元素消失了。那段时间之后一切都还好。

enter image description here

PS:我检查了代码,我认为这个问题是关系到脚本的一面。谢谢你的帮助。

回答

0

问题是在这条线与错误ID设置:

townElement.val(0); 

与jQuery与替换它的答案:

townElement.get(0).selectedIndex = 0; 
2

难道说你正在做的:

townElement.val(0); 

您的下拉值设置为0时,没有与此值的选项。您应该使用默认值中的值而不是零吗?

+0

我意识到,问题是你说行。我想设置下拉列表的第一个元素。我可以这样做:townElement.selectedIndex = 0;这工作正常。 但是,如何使用JQuery设置下拉列表的第一个元素? – kamaci 2011-02-07 12:35:31