2015-04-21 141 views
2

我正在使用jVectorMaps。默认情况下,它不允许选择的区域,除非>>regionsSelected: true在jVectorMap中,如何仅在满足条件时选择区域?

这里是我想要实现:

当用户点击他/她被提示从加载的JSON采取了问题的区域文件。如果问题的答案是正确的,我需要区域颜色才能更改仅限

我迄今为止代码:

$(function(){ 
    map = new jvm.Map({ 
    container: $('#map'), 
    map: 'world_mill_en',//map file 
    backgroundColor: ['#1E90FF'],//map background color  
    regionsSelectable:true,//this needs to be true to be able to select a region 

    onRegionClick: function(event, code){ 
     var map = $('#map').vectorMap('get', 'mapObject'); 
     var regionName = map.getRegionName(code); 
     $.getJSON('../countries.json', function(data) { 
     for (var i in data.country) { 
      if (data.country[i].name === regionName){ 
      var answer = prompt(data.country[i].question, "Type your answer here."); 
      if(data.country[i].answer === answer){ 
       alert("Correct!"); 
      }else{ 
       alert("Wrong. Try again."); 
      } 
      } 
     } 
     }); 
    }, 

    }); 
}); 

我想我已经找到一种方法来动态改变if(data.country[i].answer === answer)条件满足后的regionsSelectable的属性值。如果不是,则regionsSelectable的财产价值应保持为false

我是一个初学者,所以任何批评都比欢迎,反正我可能在这里标记! :)

回答

0

尝试了这一点..

$(function(){ 
      map = new jvm.Map({ 
      container: $('#map'), 
      map: 'world_mill_en',//map file 
      backgroundColor: ['#1E90FF'],//map background color  
      regionsSelectable:true,//this needs to be true to be able to select a region 

      onRegionClick: function(event, code){ 
       var map = $('#map').vectorMap('get', 'mapObject'); 
       var regionName = map.getRegionName(code); 
       $.getJSON('../countries.json', function(data) { 
       for (var i in data.country) { 
        if (data.country[i].name === regionName){ 
        var answer = prompt(data.country[i].question, "Type your answer here."); 
        if(data.country[i].answer === answer){ 
         map.setSelectedRegions(code);; 
        }else{ 
        map.clearSelectedRegions(); 

        } 
        } 
       } 
       }); 
      }, 

      }); 
     }); 
+0

嘿,谢谢!它现在几乎可以工作。我可以根据条件选择国家。 但是,如果我选择了5个国家(这是连续5个正确的答案),并且我回答一个错误,所有选定的国家都会被取消选择。我只需要一个国家被取消。如果你对此有任何想法,请告诉我。再次感谢 – codeWolf

+0

尝试使用方法map.getSelectedRegion()在每个区域点击前获取当前选定的区域 – CPR43

+0

谢谢,我将其标记为答案,因为它让我走上了正确的轨道 – codeWolf