Javascript为所有复选框条件的可能性选择Google标记 [英] Javascript select Google markers for all possibilities of checkbox conditions

查看:114
本文介绍了Javascript为所有复选框条件的可能性选择Google标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google地图上放置标记。我有一个复选框(目前的19)在一个窗体中,我试图创建一个动态和条件显示或不显示标记。当选中单个复选框时,代码对单个作品很有效。如果选中多个复选框,并且只显示满足所有条件的那些标记,而不只是添加满足下一个条件的那些标记,我想让代码考虑在内。



Givens:


  1. 复选框的ID为q+#

  2. 每个Google标记的值true或false为每个q#(复选框)代码如下:
    createMarker(point,html,q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11,q12,q13 ,q14,q15,q16,q17,q18,q19)

  3. 有许多标记点



如果选中复选框1和2,则只显示createMarker变量中具有q1 = true和q2 = true的标记。然而,也允许检查单个复选框和正确的标记显示
我需要这个条件为所有复选框被选中的可能性(1到19)。



 <$> 

c $ c> //循环通过复选框question
for(var h = 1; h <20; h ++){
//检查复选框是否被选中
if .getElementById('q'+ h).checked == true){
for(var i = 0; i //检查Marker通过
循环的问题的变量设置为true if(h == 1& gmarkers [i] .q1 == 1){
\\ show marker
gmarkers [i] .show();
}
if(h == 2& gmarkers [i] .q2 == 1){
//关闭测试
// gmarkers [i] 。显示();
}
if(h == 3& gmarkers [i] .q3 == 1){
// gmarkers [i] .show();
}
if(h == 4& gmarkers [i] .q4 == 1){
// gmarkers [i] .show();
}
if(h == 5& gmarkers [i] .q5 == 1){
// gmarkers [i] .show();
}
if(h == 6& gmarkers [i] .q6 == 1){
// gmarkers [i] .show();
}
if(h == 7& gmarkers [i] .q7 == 1){
// gmarkers [i] .show();
}
if(h == 8& gmarkers [i] .q8 == 1){
// gmarkers [i] .show();
}
if(h == 9& gmarkers [i] .q9 == 1){
// gmarkers [i] .show();
}
if(h == 10& gmarkers [i] .q10 == 1){
// gmarkers [i] .show();
}
if(h == 11& gmarkers [i] .q11 == 1){
// gmarkers [i] .show();
}
if(h == 12& gmarkers [i] .q12 == 1){
// gmarkers [i] .show();
}
if(h == 13& gmarkers [i] .q13 == 1){
gmarkers [i] .show();

}
if(h == 14& gmarkers [i] .q14 == 1){
// gmarkers [i] .show();
}
if(h == 15& gmarkers [i] .q15 == 1){
// gmarkers [i] .show();
}
if(h == 16& gmarkers [i] .q16 == 1){
// gmarkers [i] .show();
}
if(h == 17& gmarkers [i] .q17 == 1){
// gmarkers [i] .show();
}
if(h == 18& gmarkers [i] .q18 == 1){
// gmarkers [i] .show();
}
if(h == 19& gmarkers [i] .q19 == 1){
// gmarkers [i] .show();
}


}

}


<



strong>使用按位操作。



假设您有一个标记,属性q1,q3和q5设置为true。



而不是存储标记的每个属性,使用十进制并设置字节1,3和5。



这将导致二进制值 10101 (十进制 21 ,将其作为marker属性存储)



要比较这个十进制和选中的复选框,还要使用十进制并设置检查复选框的字节。



假设q1和q5被检查,二进制值将 10001 (十进制 17



现在要做的是检查是否一个bitwise AND 的flags属性和checkboxes-property的结果等于复选框 - 属性

 ((21& 17)>> 0 === 17)//返回true 

可处理多达32个属性。



演示 http://jsfiddle.net/doktormolle/hsPVS/


I am placing markers on a Google map. I have a number of checkboxes (19 currently) in a form that I am trying to create a dynamic "and" condition to display or not display markers. The code works great for the individual pieces when a single checkbox is selected. I would like to have the code take into account if multiple checkboxes are selected and only display those markers that meet all conditions not just add the ones that meet the next condition.

Givens:

  1. The checkbox ids are "q"+#
  2. Each Google Marker has values of true or false for each q#(checkbox) the code is below: createMarker(point,html,q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11, q12, q13, q14, q15, q16, q17, q18, q19)
  3. There are many Marker Points

Need:

If checkbox 1 and 2 are checked only display those markers that have q1 = true "and" q2 = true in the createMarker variables. However, also allow for a single checkbox to be checked and the correct Markers displayed I need this condition for all possibilities of checkboxes being selected (1 to 19).

How can I make this work?

Current Code for single condition:

//loop through the checkbox questions
for (var h=1; h<20; h++) {
        //check to see if the checkbox is checked
        if (document.getElementById('q'+h).checked == true) {               
            for (var i=0; i<gmarkers.length; i++) { 
                //check to see if the Marker has that variable set to true for the question it is loop through                  
                if (h == 1 && gmarkers[i].q1 == 1) {                            
                                    \\ show marker                      
                                     gmarkers[i].show();                    
                }
                if (h == 2 && gmarkers[i].q2 == 1) {                            
                   // turned off for testing    
                                       //gmarkers[i].show();                    
                }
                if (h == 3 && gmarkers[i].q3 == 1) {                            
                    //gmarkers[i].show();                   
                }
                if (h == 4 && gmarkers[i].q4 == 1) {                            
                    //gmarkers[i].show();                   
                }
                if (h == 5 && gmarkers[i].q5 == 1) {                            
                    //gmarkers[i].show();                   
                }
                if (h == 6 && gmarkers[i].q6 == 1) {                            
                    //gmarkers[i].show();                   
                }
                if (h == 7 && gmarkers[i].q7 == 1) {                            
                    //gmarkers[i].show();                   
                }
                if (h == 8 && gmarkers[i].q8 == 1) {                            
                    //gmarkers[i].show();                   
                }
                if (h == 9 && gmarkers[i].q9 == 1) {                            
                    //gmarkers[i].show();                   
                }
                if (h == 10 && gmarkers[i].q10 == 1) {                          
                    //gmarkers[i].show();                   
                }
                if (h == 11 && gmarkers[i].q11 == 1) {                          
                    //gmarkers[i].show();                   
                }
                if (h == 12 && gmarkers[i].q12 == 1) {                          
                    //gmarkers[i].show();                   
                }
                if (h == 13 && gmarkers[i].q13 == 1) {                          
                    gmarkers[i].show();

                }
                if (h == 14 && gmarkers[i].q14 == 1) {                          
                    //gmarkers[i].show();                   
                }
                if (h == 15 && gmarkers[i].q15 == 1) {                          
                    //gmarkers[i].show();                   
                }
                if (h == 16 && gmarkers[i].q16 == 1) {                          
                    //gmarkers[i].show();                   
                }
                if (h == 17 && gmarkers[i].q17 == 1) {                          
                    //gmarkers[i].show();                   
                }
                if (h == 18 && gmarkers[i].q18 == 1) {                          
                    //gmarkers[i].show();                   
                }                   
                if (h == 19 && gmarkers[i].q19 == 1) {                          
                    //gmarkers[i].show();           
                }


            }

        }

解决方案

An approach where you did not have to iterate over each q-property of the marker:

Use bitwise operations.

Lets assume you have a marker with the properties q1, q3 and q5 set to true .

Instead of storing each property of the marker, use a decimal and set the bytes 1,3 and 5.

This would result in the binary value 10101 (the decimal is 21, store it as the marker-property)

To compare this decimal with the checked checkboxes use also a decimal and set the bytes where the checkboxes are checked.

Assuming the checkboxes for q1 and q5 are checked, the binary value would be 10001(decimal 17)

All you have to do now is to check if the result of a bitwise AND of the markers property and the checkboxes-property is equal to the checkboxes-property

((21 & 17)>>>0===17)//returns true

This approach would work for up to 32 properties.

Demo: http://jsfiddle.net/doktormolle/hsPVS/

这篇关于Javascript为所有复选框条件的可能性选择Google标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆