使用jquery按文本选择选项
我在我的最新项目中使用谷歌地图,我有一个包含很多地方的组合框,
当我的页面加载时,这个组合有两个属性(经度,纬度)我有绘制标记的功能此组合中的所有地方此功能都在这里:
i use google map in my newest project ,and i have a combo box which contain a lot of places , this combo has two attributes(longitude,latitude) when my page loads i have function that plot markers for all places within this combo this function is here :
//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上工作正常,当我点击标记第一次放大到选定的位置,如果我选择了(索引0)的(选择)标记,则调用此函数再次绘制所有位置并缩小地图以显示所有位置(直到这里每件事都很好)但如果我再次点击相同的标记,它什么都不做!甚至不把标记标题的内容放在我的组合框这一行:
this code works very fine on internet explorer ,put with firefox when i click the marker for first time it zoom in to the selected place and if i have selected (index 0) of (select) tag it call this function to plot all places again and zoom out of map to show them all (till here every thing is fine) but if i have clicked the same marker again it do nothing ! don't even put content of marker title at my combo box at this line :
$('#moh option:[text="' + marker.title + '"]').attr('selected', true);
让我感到紧张的是,这段代码非常适用于IE !!!!
what makes me nervous that this code works very will on IE !!!!
在jQuery 1.6中更改了attr属性:
看到这个问题: .prop()vs .attr()
The attr property was changed in jQuery 1.6:
See this question: .prop() vs .attr()