2015-04-21 42 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