Is it possible to get the value of a lt;tdgt; element using onclick?(是否有可能得到一个 lt;tdgt; 的值?使用 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();
这篇关于是否有可能得到一个 <td> 的值?使用 onclick 的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否有可能得到一个 <td> 的值?使用


- 400或500级别的HTTP响应 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Flexslider 箭头未正确显示 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01