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

查看:81
本文介绍了是否有可能获得< 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天全站免登陆