php:onClick事件-表

时间:2019-05-13 06:59:17

标签: php mysql

如何在表格中使用onclick? 以下代码是我的基本代码。

php代码

$con = mysqli_connect($db_host,$db_user,$db_passwd,$db_name);
mysqli_set_charset($con, "utf8");

$result = mysqli_query($con, "select * from Marker_DB");

echo "<table border=’1′> <tr> <th>num</th> </tr>";

$n = 1;
while($row = mysqli_fetch_array($result)){
  echo "<tr>";
  echo "<td>". $row['num'] . "</td>";
  echo "</tr>";
$n++;
}

echo "</table>";
mysqli_close($con);

我已经做了很多尝试使用onclick的尝试。这是我尝试的方法:

代码

echo "<td>". "onclick='displayComment($row['num'])" . "</td>";

echo "<td onClick="'www.google.com/'">". $row['num'] . "</td>";

如何在php中使用onclick?

2 个答案:

答案 0 :(得分:1)

为您的表提供一些ID,

echo "<table border='1′ id='table-id'> <tr> <th>num</th> </tr>";

然后您的td将保持不变,

echo "<td>". $row['num'] . "</td>";

在js中,只需编写此代码并检查,

$("#table-id").on("click", "td", function() {
    alert($(this).text()); // see if you get the value of $row['num']
    // you can play here as you like
    var num = $(this).text();
    displayComment(num);
});

function displayComment(num) {
    // your code
}

答案 1 :(得分:0)

您需要做的是在单击孩子时停止传播父事件,这在jQuery中很容易完成,但是天真的您需要做更多的工作:

#include <stdio.h>
#include <stdlib.h>
#define num 10

    void ler_matriz(int **matriz1, int n, int m);
    void mostrar_matriz(int** matriz1, int n, int m);
//int num_min_matriz(int matriz1[][], int n, int m);
//void teste_simetria(int matriz1[][], int n, int m);
//void transposta_matriz(int matriz1[][], int n, int m);
//void soma_matriz(int matriz1[][], int matriz2[][], int matriz3[][], int n, int m);

int main()
{

    //int x[num][num], y[num][num], z[num][num], numL, numC;
    int i,j,**x,**y,**z,numL,numC; //Proper declarations
    x=malloc(num*sizeof(int*));
    y=malloc(num*sizeof(int*));
    z=malloc(num*sizeof(int*));
    for(i=0;i<num;i++) {
        x[i]=malloc(num*sizeof(int));
        y[i]=malloc(num*sizeof(int));
        z[i]=malloc(num*sizeof(int));

    }
    //Initialization
    for(i=0;i<num;i++) {
        for(j=0;j<num;j++) {
            x[i][j]=y[i][j]=z[i][j]=0;
        }
    }
    printf("Introduza o número de linhas e colunas para a matriz:\n");
    scanf(" %d%d", &numL, &numC);
    printf("\n\nIntroduza os valores para a matriz 1:   ");
    ler_matriz(x, numL, numC);
    mostrar_matriz(x, numL, numC);
    for(i=0;i<num;i++) {
        free(x[i]);
        free(y[i]);
        free(z[i]);
    }
    free(x);
    free(y);
    free(z);
    return 0;
}



void ler_matriz(int **matriz1, int n, int m)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            printf("\nx[%d][%d]:    ", i + 1, j + 1);
            scanf(" %d", &matriz1[i][j]); 
        }
    }
}

void mostrar_matriz(int **matriz1, int n, int m)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            printf("%d     ", matriz1[i][j]);
        }
        putchar('\n');
    }
}

注意,对于Firefox,您需要传递一个事件参数:

function displayComment(num){ 
    //Do your stuffs with num

    console.log('td clicked');
};