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.

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

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)