drde3456 2017-06-07 09:16
浏览 36
已采纳

如何在多个div之间只进行两个div可点击并将其内容发送到php文件

I have this php file which is fetch data from my database and place it into divs. this div created by while loop I want tow div only to be select then send the content to php file using Ajax. The code execute with no problem but there is no result!! Any help advance and I completely lost of mind !!!

while($row=mysqli_fetch_array($result)){

    $so=$row[0];
    $s=$row[1];
    $m=$row[2];
    $a=$row[3];
    $b=$row[5];
    $c=$row[8];

?>
 <body>
 <div id="all"> //start all
 <div class="r"> //start r 
  <div id="e"><?php echo"{$s}"; ?></div>
  <div id="b"><?php echo"{$m}"; ?></div>
  <div id="a"><?php echo"{$a}"; ?></div>
  <div id="b"><?php echo"{$b}"; ?></div>
 </div> //end r
 </div> //end all
  <?php
  } //end while
  ?>
  <div id="show"></div>

script code

$(document).ready(function(){
         $(".r").click(function(){

          $data = $(this).text();
          alert($data); //it work well
               $.ajax({
               url: "view.php",
               type: "post",
               data: {
               you: $data
                     },
                     success:function(response){
                     $('#show').html(response);
                     }  
               });

           });
       });

view.php

<?php
if(isset($_post['you'])){
printf("ok");   //just to check
}
?>
  • 写回答

1条回答 默认 最新

  • douyi5822 2017-06-07 10:23
    关注

    First of all

    1. IDs on a page should be unique

    Now coming to your code, maintain a global variable to have select count and call the below function to allow selection of maximum 2 divs

    var selectCount = 0;
    $('.r div').click(function() {
        if(!$(this).hasClass('active') && selectCount < 2) {
            $(this).addClass('active');
            selectCount++;
        }
        else if($(this).hasClass('active')) {
            $(this).removeClass('active');
            selectCount--;
        }
        else {
            alert("ony 2 allowed");
        }
    });
    

    To send the data to your call you can

    $('button').click(function() {
        var result = [];
        $('.active').each(function(val){
            result.push(val);
        });
    });
    

    You may change the data format accordingly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题