简体   繁体   English

在 php 中单击时,如何使表格在 javascript 中做出反应?

[英]How to make a table react in javascript when clicked in php?

<?php>
if (isset($_POST['search']))
{
    $startDate = $_POST['start'];
    $startDate = str_replace('/', '-', $startDate ); 
    $startDate = date("Y-m-d", strtotime($startDate));
    echo $startDate;
    
    $endDate = $_POST['end'];
    $endDate = str_replace('/', '-', $endDate ); 
    $endDate = date("Y-m-d", strtotime($endDate));
    echo $endDate;

    $model = $_POST['model'];
    echo $model;

    $dates = getDatesStartToLast($startDate, $endDate);



    for ($i=0; $i < count($dates); $i++){
        echo "<tr>";
        echo "<td><text class = 'dateselect'>$dates[$i]</text></td>";
        
        $query = mysqli_query($conn, "SELECT * from electec_db where DateInputTime >= '$dates[$i]' and DateInputTime <= '$dates[$i] 23:59:59' and Model = '$model'");
        $count = mysqli_num_rows($query);
        // echo $count;
        echo "<td>$count</td>";

        $arrays = array("Result = 'NG'", "Species = 'Burr'", "Species = 'Dirt'", "Species = 'Scratch'", "Species = 'Cracked'", "Species = 'Gas'" );
        for ($j=0; $j < count($arrays); $j ++){
            $query = mysqli_query($conn, "SELECT * from electec_db where DateInputTime >= '$dates[$i]' and DateInputTime <= '$dates[$i] 23:59:59' and Model = '$model' and $arrays[$j]");
            $count = mysqli_num_rows($query);
            echo "<td>$count</td>";

// And this is jsp code
<script>
$('.dateselect').click(function() { 
    var dateSelect = $(this).text();
    alert(dataSelect)

}
</script>

This is php code and javascript code.这是 php 代码和 javascript 代码。 When I click echo "$dates[$i]";当我点击 echo "$dates[$i]"; part, I want to make react in script like alert dateSelect variable.部分,我想在脚本中做出反应,如警报 dateSelect 变量。 But it doesn't make any reaction.但它没有任何反应。 How to make a table react in javascript when clicked in php?在 php 中单击时,如何使表格在 javascript 中做出反应?

Do you have jQuery installed in your project?您的项目中是否安装了 jQuery? That's what the $ indicates.. Not sure what the jsp comment is about.这就是 $ 所表示的......不知道 jsp 评论是关于什么的。

In straight javascript you could do something like this:在直接 javascript 中,您可以执行以下操作:

 document.querySelectorAll('.dateSelect').forEach(item => { item.addEventListener('click', event => { alert(event.target.innerHTML) }) })
 <p class="dateSelect">thing1</p> <p class="dateSelect">thing2</p> <p class="dateSelect">thing3</p>

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

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