使用jquery按文本选择选项

时间:2011-05-09 20:02:29

标签: jquery google-maps select

我在我最新的项目中使用谷歌地图,我有一个包含很多地方的组合框, 当我的页面加载时,这个组合有两个属性(经度,纬度)我有这个函数的所有位置的标记标记这个函数在这里:

//Set All mohafzat markers
    function setMohMarkers(){
    //Loop Through mohafzat Combo
    $("#moh").each(function(i){
    //Remove Old Markers
            //clearOverlays();
            //loop Through it's Options
            $('option',this).each(function(i){
            var midx=$(this).index();
            if(midx==0){
                //Don't Plot 0 Index item

            }else{
            var idx=$(this).index();//get Current Index
            var lon=$(this).attr('lng');
            var lat=$(this).attr('lat');
            var mname=$(this).text();
            //point's On Map
            var myLatlng = new google.maps.LatLng(lat,lon);

            //put Marker
            var marker = new google.maps.Marker({
              position: myLatlng, 
              map: map, 
              icon:image,
             //animation: google.maps.Animation.BOUNCE,
              title:mname
              }); 


             google.maps.event.addListener(marker, 'click', function() {
                map.setZoom(10);
                map.setCenter(marker.latlng);
                //Set mohafzat Combo to selected Marker
                //$("#moh option:contains(" + marker.title +")").attr("selected","selected");
                $('#moh option:[text="' + marker.title + '"]').attr('selected', true);

                //Trigger Change Function

                $("#moh option:contains(" + marker.title +")").trigger('change');

                });
                //push marker To my array(help me of deleting them :D)
                Allmarkers.push(marker);
            }});
            //End Click Function
    });
    }

这个代码在Internet Explorer上运行得非常好,当我第一次点击标记放大到所选位置时放入firefox,如果我选择了(索引0)(select)标签,则调用此函数来绘制所有的地方再次和缩小地图,以显示所有(直到这里每件事情都很好),但如果我再次点击相同的标记,它什么都不做!甚至不把标记标题的内容放在我的组合框这一行:

$('#moh option:[text="' + marker.title + '"]').attr('selected', true);

让我感到紧张的是,这段代码非常适用于IE !!!!

1 个答案:

答案 0 :(得分:3)

在jQuery 1.6中改变了attr属性:
请参阅此问题:.prop() vs .attr()

相关问题