找到你要的答案

Q:Php foreach and echo inside javascript

Q:在JavaScript php foreach和回声

Good day to everyone:). I just want to ask how can I do php foreach and echo values inside the javascript, because I want the javascript to be a dynamic. Here is the static js

<script type="text/javascript">
$(document).ready(function(){
    $('input[type="checkbox"]').click(function(){


        if($(this).attr("value")=="1"){
            $(".1").toggle();
        }
        if($(this).attr("value")=="2"){
            $(".2").toggle();
        }
        if($(this).attr("value")=="3"){
            $(".3").toggle();
        }
        if($(this).attr("value")=="4"){
            $(".4").toggle();
        }

    });
});
</script>

and this what I want to happen

<script type="text/javascript">
$(document).ready(function(){
    $('input[type="checkbox"]').click(function(){

<?php foreach($data as $field): ?> 

        if($(this).attr("value")=="<?php echo $field->ID);?>" ){
            $(".<?php echo $field->ID);?>").toggle();
        }

<?php endforeach; ?>       
    });
});
</script>  

the ID values form the database are 1,2,3,4. That would be all. Thank You!

Good day to everyone:). I just want to ask how can I do php foreach and echo values inside the javascript, because I want the javascript to be a dynamic. Here is the static js

<script type="text/javascript">
$(document).ready(function(){
    $('input[type="checkbox"]').click(function(){


        if($(this).attr("value")=="1"){
            $(".1").toggle();
        }
        if($(this).attr("value")=="2"){
            $(".2").toggle();
        }
        if($(this).attr("value")=="3"){
            $(".3").toggle();
        }
        if($(this).attr("value")=="4"){
            $(".4").toggle();
        }

    });
});
</script>

这就是我想要发生的

<script type="text/javascript">
$(document).ready(function(){
    $('input[type="checkbox"]').click(function(){

<?php foreach($data as $field): ?> 

        if($(this).attr("value")=="<?php echo $field->ID);?>" ){
            $(".<?php echo $field->ID);?>").toggle();
        }

<?php endforeach; ?>       
    });
});
</script>  

ID值形式的数据库是1,2,3,4。那将是全部。谢谢您!

answer1: 回答1:

you can push all the values to a js array and than do a loop on js array.

<script type="text/javascript">
$(document).ready(function(){
    my_values = Array;
    <?php foreach($data as $field): ?> 
        my_values.push ("<?php echo $field; ?>");
    <?php endforeach; ?>

    $('input[type="checkbox"]').click(function(){
        // java script loop here
        for ( var i in my_values)
        {
          if($(this).attr("value")== i )
         {
           $("."+this.value).toggle();
         }
      } 
    });
});

</script>   

你可以把所有的价值比JS数组和JS数组循环。

<script type="text/javascript">
$(document).ready(function(){
    my_values = Array;
    <?php foreach($data as $field): ?> 
        my_values.push ("<?php echo $field; ?>");
    <?php endforeach; ?>

    $('input[type="checkbox"]').click(function(){
        // java script loop here
        for ( var i in my_values)
        {
          if($(this).attr("value")== i )
         {
           $("."+this.value).toggle();
         }
      } 
    });
});

</script>   
javascript  php