javascript - Row Onclick 函数无法工作可能是因为 ajax 表

标签 javascript php jquery mysql ajax

我现在可以显示我的表格,因为我使用 ajax 将表格从我的 php 文件回显到我的 html 索引文件中。但现在,我需要在单击时显示一个警报,它会告诉我接下来几列的值。我想点击类(class),它会在警报中告诉我接下来几个季度的申请者数量(通过 javascript 函数)。

索引:

function gettable(string,strings)
        {

var a=string;
var b=strings;
if(a == null && b == null)
{
a = new Date().getFullYear();
b = "";
}

    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        var xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("inputtable").innerHTML = xmlhttp.responseText;

        }
    };

    xmlhttp.open("GET","gettabledata.php?q="+a+"&v="+b,true);
    xmlhttp.send();
        }

var name;
$(document).ready(function()
{
$('#table tr #abc').on('click', function(){

    name = $(this).text();
    alert(name);
});
})

gettabledata.php(我回显表格的文件):

    <?php

error_reporting(0);
$q = intval($_GET['q']);
$v = $_GET['v'];



if ($q=="")
{$i=date("Y");}
else
{
    $i=$q;
}
$i0=$i.'-01-01';

$i1=$i.'-03-31';
$i2=$i.'-06-31';
$i3=$i.'-09-31';
$i4=$i.'-12-31';

echo "<table id='table' class='clickable-row' border='1' style='width:100%'>
<tr id='header'>
<th>Courses</th>
<th>Quarter 1</th>
<th>Quarter 2</th>
<th>Quarter 3</th>
<th>Quarter 4</th>
<th>Total</th>
<th>Change In Volume (Compared to previous year)</th>
</tr>";

$con = mysqli_connect('localhost','root','','hi')or die ("Error :". mysqli_connect_error());

$output="";

if (!isset($_GET['v']))
{

$qry5 = "SELECT Category FROM hi.courses GROUP BY Category;";

$result5 = mysqli_query($con,$qry5);
while($row=mysqli_fetch_assoc($result5)){
    $var5=$row['Category'];

            $qry1 = "SELECT COUNT(*) FROM courses WHERE Category LIKE '$var5' AND Date BETWEEN '$i0' AND '$i1'";

            $result1 = mysqli_query($con,$qry1);

            $qry2 = "SELECT COUNT(*) FROM courses WHERE Category LIKE '$var5' AND Date<='$i2' AND Date>'$i1'";

            $result2 = mysqli_query($con,$qry2);

            $qry3 = "SELECT COUNT(*) FROM courses WHERE Category LIKE '$var5' AND Date<='$i3' AND Date>'$i2'";

            $result3 = mysqli_query($con,$qry3);

            $qry4 = "SELECT COUNT(*) FROM courses WHERE Category LIKE '$var5' AND Date<='$i4' AND Date>'$i3'";

            $result4 = mysqli_query($con,$qry4);

            $qry0 = "SELECT COUNT(*) FROM courses WHERE Category LIKE '$var5' AND year(Date)='$i'-1";
            $result0 = mysqli_query($con,$qry0);

            while($row=mysqli_fetch_assoc($result0)){
            $count0 = $row['COUNT(*)'];
            }

            while($row=mysqli_fetch_assoc($result1)){
            $count1 = $row['COUNT(*)'];
        }
        while($row=mysqli_fetch_assoc($result2)){
            $count2 = $row['COUNT(*)'];
        }
        while($row=mysqli_fetch_assoc($result3)){
            $count3 = $row['COUNT(*)'];
        }
        while($row=mysqli_fetch_assoc($result4)){
            $count4 = $row['COUNT(*)'];
        }
            $arrow="";
$space="<span>&nbsp;</span><span>&nbsp;</span>";
            $Total=$count1+$count2+$count3+$count4;
            if($Total<$count0)
            {
                $Volume=(($count0-$Total)/$count0)*100;
                $RevisedVolume= number_format($Volume, 2, '.', '');
                $arrow="<b><span style='color:Red;font-size: 150%;'>&#8595;</span></b>";
            }
            else if($Total>$count0)
            {
                $Volume=(($Total-$count0)/$Total)*100;
                $RevisedVolume= number_format($Volume, 2, '.', '');
                $arrow="<b><span style='color:#00FF00;font-size: 150%;'>&#8593;</span></b>";
            }
            else 
            {
                $Volume="No Change in volume";
                $arrow="";
            }

            if(is_numeric($Volume))
            {
                $RevisedVolume.="&#37;";
            }


            $output="
<tr id='table'>
<td class='cat' id='abc' value='$var5'>$var5</td>
<td class='cnt1' value='$count1'>$count1</td>
<td class='cnt2' value='$count2'>$count2</td>
<td class='cnt3' value='$count3'>$count3</td>
<td class='cnt4' value='$count4'>$count4</td>
<td class='cnt5' value='$Total'>$Total</td>
<td class='cnt6' value='$RevisedVolume'>$RevisedVolume$space$arrow</td>
</tr>";
            echo $output;
}

}

            else
            {

            $qry5 = "SELECT Category FROM hi.courses WHERE Category LIKE '$v%' GROUP BY Category;";

            $result5 = mysqli_query($con,$qry5);
            while($row=mysqli_fetch_assoc($result5)){
            $var5=$row['Category'];


            $qry1 = "SELECT COUNT(*) FROM courses WHERE Category LIKE '%$var5%' AND Date BETWEEN '$i0' AND '$i1'";

            $result1 = mysqli_query($con,$qry1);

            $qry2 = "SELECT COUNT(*) FROM courses WHERE Category LIKE '%$var5%' AND Date<='$i2' AND Date>'$i1'";

            $result2 = mysqli_query($con,$qry2);

            $qry3 = "SELECT COUNT(*) FROM courses WHERE Category LIKE '%$var5%' AND Date<='$i3' AND Date>'$i2'";

            $result3 = mysqli_query($con,$qry3);

            $qry4 = "SELECT COUNT(*) FROM courses WHERE Category LIKE '%$var5%' AND Date<='$i4' AND Date>'$i3'";

            $result4 = mysqli_query($con,$qry4);

            $qry0 = "SELECT COUNT(*) FROM courses WHERE Category LIKE '%$var5%' AND year(Date)='$i'-1";
            $result0 = mysqli_query($con,$qry0);

            while($row=mysqli_fetch_assoc($result0)){
            $count0 = $row['COUNT(*)'];
            }


            while($row=mysqli_fetch_assoc($result1)){
            $count1 = $row['COUNT(*)'];
        }
        while($row=mysqli_fetch_assoc($result2)){
            $count2 = $row['COUNT(*)'];
        }
        while($row=mysqli_fetch_assoc($result3)){
            $count3 = $row['COUNT(*)'];
        }
        while($row=mysqli_fetch_assoc($result4)){
            $count4 = $row['COUNT(*)'];
        }



            $arrow="";
            $space="<span>&nbsp;</span><span>&nbsp;</span>";
            $Total=$count1+$count2+$count3+$count4;
            if($Total<$count0)
            {
                $Volume=(($count0-$Total)/$count0)*100;
                $RevisedVolume= number_format($Volume, 2, '.', '');
                $arrow="<b><span style='color:Red;font-size: 150%;'>&#8595;</span></b>";
            }
            else if($Total>$count0)
            {
                $Volume=(($Total-$count0)/$Total)*100;
                $RevisedVolume= number_format($Volume, 2, '.', '');
                $arrow="<b><span style='color:#00FF00;font-size: 150%;'>&#8593;</span></b>";
            }
            else 
            {
                $Volume="No Change in volume";
                $arrow="";
            }

            if(is_numeric($Volume))
            {
                $RevisedVolume.="&#37;";
            }


            $output="
<tr id='table'>
<td class='cat' value='$var5'>$var5</td>
<td class='cnt1' value='$count1'>$count1</td>
<td class='cnt2' value='$count2'>$count2</td>
<td class='cnt3' value='$count3'>$count3</td>
<td class='cnt4' value='$count4'>$count4</td>
<td class='cnt5' value='$Total'>$Total</td>
<td class='cnt6' value='$RevisedVolume'>$RevisedVolume$space$arrow</td>
</tr>";

        echo $output;

        }
}

最佳答案

id='table' 在您的代码中重复。请从 tr 中删除 id = "table"

并且您需要修改 onClick 函数,例如...

$('#inputtable').on('click', '#table tr #abc',function(){

我的事。对你有帮助......

关于javascript - Row Onclick 函数无法工作可能是因为 ajax 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38326443/

相关文章:

javascript - 根据 Javascript,DIV 的宽度不是什么,为什么?

JavaScript - 这个 if 语句中的条件有什么意义 : if(document. getElementById)

javascript - 使用 jQuery 更改 twig foreach 循环中的类

javascript - 单击按钮无法在 laravel ajax 中下载文件

php - jquery发送带有附件的电子邮件

javascript - 在组件本身之外渲染组件的一部分

javascript - Php Storm "Let definition are not supported by current JavaScript version"

php - 如何在redis中对这种数据结构进行建模?

php - 我的 PHP session 在我登录时给出了错误的输出

javascript - 我怎样才能让这个电子邮件功能发挥作用?