是否有可能得到一个 <td> 的值?使用 onclick 的元素? [英] Is it possible to get the value of a <td> element using onclick?

查看:20
本文介绍了是否有可能得到一个 <td> 的值?使用 onclick 的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个表格,其中包含使用 php 回显的电子邮件模板名称列表.下面是部分php代码.我正在尝试获取表值并将其传递给我的 JS 文件,未来的 AJAX 命令会将其传递给另一个文件(我不会有任何问题).我第一次尝试提醒该值表明该值未定义.我的第二次尝试显示了它内部的元素类型(当时它是一个跨度).现在它什么也没有显示.有什么建议吗?

I currently have a table that has a list of email template names being echoed using php. Below is part of the php code. I'm trying to grab the table value and pass it to my JS file where a future AJAX command will pass it to a different file (that I won't have any issues with). My first attempt to alert out the value stated that the value was undefined. My second attempt showed the type of element it was inside (at the time it was a span). Now it's not showing anything. Suggestions?

PHP 代码:

<table class="departments">
<tr>
<th scope="col" style="width: 175px;">Email Name</th>
';
$get_depts = mysql_query("SELECT dept_name FROM depts where bus_id = '{$_SESSION['bus_id']}'");
while(($department = mysql_fetch_assoc($get_depts)))
{
    echo '
    <th scope="col" style="width: 175px;">'.$department['dept_name'].'</th>
    ';
}
echo '
</tr>
';
$get_emails = mysql_query("SELECT id, email_name from emails where bus_id = '{$_SESSION['bus_id']}' ORDER BY email_name ASC");
while(($email = mysql_fetch_assoc($get_emails)))
{
    echo '
    <tr>
    <td id="test" onclick="moveValue()">'.$email['email_name'].'</td>
    ';

当前 JS 代码:

function moveValue()
{
var x = document.getElementById(test);
var y = x.innerHTML;
alert(y);
} 

推荐答案

Javascript:

Javascript:

var y = document.getElementById("test").innerText;

jQuery:

$("#test").text();

获取 HTML:

var html = document.getElementById("test" ).innerHTML;

jQuery:

$("#test").html();

这篇关于是否有可能得到一个 &lt;td&gt; 的值?使用 onclick 的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆