Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jVectorMap中,如何仅在满足条件时选择区域?_Javascript_Jquery_Jvectormap - Fatal编程技术网

Javascript 在jVectorMap中,如何仅在满足条件时选择区域?

Javascript 在jVectorMap中,如何仅在满足条件时选择区域?,javascript,jquery,jvectormap,Javascript,Jquery,Jvectormap,我正在使用jVectorMaps。默认情况下,它不允许选择区域,除非>>regionselected:true 以下是我努力实现的目标: 当用户单击某个区域时,他/她会收到来自加载的JSON文件的问题提示。如果问题的答案正确,我只需要更改区域颜色 到目前为止,我的代码是: $(function(){ map = new jvm.Map({ container: $('#map'), map: 'world_mill_en',//map file backgroundC

我正在使用jVectorMaps。默认情况下,它不允许选择区域,除非>>
regionselected:true

以下是我努力实现的目标:

当用户单击某个区域时,他/她会收到来自加载的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)
条件后,动态更改
regionselective
的属性值。如果不是,则
regionselectable
的属性值应保持为
false

我是一个初学者,所以任何批评都是非常受欢迎的,不管怎样,我可能在这里偏离了目标!:)

试试这个

 $(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();

                    }
                  }
                }
              });
            },

          });
        });

嘿,谢谢!现在几乎可以用了。我可以根据情况选择国家。但是如果我选择了5个国家(即连续5个正确答案),并且我回答了一个错误,那么所有选择的国家都会被取消选择。我只需要取消选择一个国家。如果你对此有任何想法,请告诉我。再次感谢Intry方法map.getSelectedRegion()要在单击每个区域之前获取当前选定的区域谢谢,我会将此标记为答案,因为它让我走上了正确的轨道