简体   繁体   English

如何将PHP回显DIV值传递到对话框

[英]how to pass PHP echo DIV values to dialog box

Each articles are located in DIV tag. 每个文章都位于DIV标签中。 Want to make each DIV clickable and once i click DIV, dialog box should display with appropriate DVI values $add->tbl_article_content , $add->tbl_article_image my JS dialog box appear for only 1st DIV. 想要使每个DIV可单击,并且一旦我单击DIV,对话框应显示适当的DVI$add->tbl_article_content$add->tbl_article_image我的JS对话框仅出现在第一个DIV中。 how should i do it for all DIV and pass relevant data. 我应该如何对所有DIV进行处理并传递相关数据。

PHP 的PHP

<div class="row">
<?php
    foreach ($data as $value) {
       echo "<div class='col-lg-3'>";
       echo "<p id='target'>" . $value->tbl_article_header . "</p>";
       echo "</div>";       
}
?>

Jquery jQuery的

$( "#target" ).click(function() {
        alert( "relevant DIV database values ??? " );
    });

Instead of target as an id you should make it as class 而不是将target作为ID,您应该将其作为class

<div class="row">
<?php
    foreach ($data as $value) {
       echo "<div class='col-lg-3'>";
       echo "<p class='target'>" . $value->tbl_article_header . "</p>";
       echo "</div>";       
   }
?>

 $( ".target" ).click(function() { alert( $(this).html() ); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class='col-lg-3'> <p class='target'>Value 1</p> </div> <div class='col-lg-3'> <p class='target'>Value 2</p> </div> <div class='col-lg-3'> <p class='target'>Value 3</p> </div> 

UPDATE 更新

<div class="row">
<?php
    foreach ($data as $value) {
       echo "<div class='col-lg-3'>";
       echo "<p class='target' data-article=".$add->tbl_article_content.">" . $value->tbl_article_header . "</p>";
       echo "</div>";       
   }
?>

 $( ".target" ).click(function() { alert( $(this).attr('data-article') ); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class='col-lg-3'> <p class='target' data-article="Articel 1">Head 1</p> </div> <div class='col-lg-3'> <p class='target' data-article="Articel 2">Head 2</p> </div> <div class='col-lg-3'> <p class='target' data-article="Articel 3">Head 3</p> </div> 

the id is unique inside a page ... use a class for a group of related div id在页面内是唯一的...对一组相关的div使用一个类

<?php
foreach ($data as $value) {
   echo "<div class='col-lg-3'>";
   echo "<p class='target'>" . $value->tbl_article_header . "</p>";
   echo "</div>";       
}
?>

and for js 和js

 $( ".target" ).click(function() {
    alert( "relevant DIV database values ??? " );
  }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM