简体   繁体   English

分配Javascript变量以更正HTML表的文本值( <td> 标签)

[英]Assign Javascript variable to correct text value of HTML table (The <td> tag)

I have a PHP file that outputs an HTML table. 我有一个输出HTML表的PHP文件。 The table is dynamically created from a list of music videos in a folder. 该表是根据文件夹中的音乐视频列表动态创建的。 It then outputs my table with the data. 然后输出带有数据的我的表。

I need to assign a Javascript variable for the td where I am typing to a Javascript function. 我需要为要在其中键入Javascript函数的td分配Javascript变量。 I window.alert to test the variable but it is blank. 我在window.alert中测试该变量,但它为空。 I think it is because it doesn't uniquely identify the record I am busy with 我认为是因为它不能唯一地标识我正在处理的记录

How can I uniquely identify the text? 如何唯一标识文本?

I have tried: 我努力了:

var y = document.getElementById("about");
var y = document.getElementById("about").value;
var y = document.getElementById("about").innerHTML;
var y = document.getElementById("about").innerText;

It doesn't seem to work, here is my table: 它似乎不起作用,这是我的桌子:

$output .= '<div id="videos-div" class="videos">'."\n";
$output .= '  <table class="stats" cellspacing="5">'."\n";
$output .= ' <tr>'."\n";
$output .= '   <th class="hed" colspan="1">VideoName</td>'."\n";
$output .= '   <th class="hed" colspan="1">Date</td>'."\n";
$output .= '   <th class="hed" colspan="1">Description</td>'."\n";
$output .= ' </tr>'."\n";

Now for each music video I: 现在,对于每个音乐视频,我:

$output .= '<tr id="actiontable'.$record['video'].'">'."\n";
$output .= ' <td>'.$videoname.'</td>'."\n";
$output .= ' <td>'.$date.' min</td>'."\n";
$output .= ' <td id="about" ><input type="text" 
oninput="myFunction()" 
placeholder="'.$metadata2.'" /></td>'."\n";

My function: 我的功能:

$output .= '
<script type="text/javascript">
function myFunction ()
{
var y = document.getElementById("about");
window.alert(y);
}
</script>';

When I hardcode the: window.alert it does work 当我对window.alert硬编码时,它确实起作用

What I need to do is take the text from the: Description and then pass that to another Javascript function 我需要做的是从描述中获取文本,然后将其传递给另一个Javascript function

I don't get any errors, just a blank variable . 我没有任何错误,只是一个blank variable I think it is because it doesn't know it is for the td where I am typing 我认为是因为它不知道我输入的是TD

在两个位置进行更改,将id应用于输入类型,并在通过ID获得之后第二个放置'.value'

  1. <input id="about" type="text" oninput="myFunction()" placeholder="'.$metadata2.'" /></td>

  2. var y = document.getElementById("about").value;

First you have to be sure that your HTML as been rendered, so go on your console and look that <td id="about"> is here. 首先,您必须确保已呈现HTML,因此请在控制台上查看<td id="about">在此处。

If yes your function with the code under should display something like [object HTMLTdElement] . 如果是,则带有代码的函数应显示类似[object HTMLTdElement]

var y = document.getElementById("about");
alert(y); 

Second When you try all of this: 其次,当您尝试所有这些时:

var y = document.getElementById("about");
var y = document.getElementById("about").value;
var y = document.getElementById("about").innerHTML;
var y = document.getElementById("about").innerText;

It's done on the <td> tag, who have the id , but you want to do it into the input child. 这是在<td>标记上完成的,该标记具有id ,但是您想要在input子代中进行。 Do it like that: var y = document.querySelector("#about input").value 这样做: var y = document.querySelector("#about input").value

And it should works ! 而且应该有效!

Edit: I just realized a different problem. 编辑:我刚刚意识到一个不同的问题。 id s have to be unique, but you are adding another about with every new <tr> . id ■找是唯一的,但要添加其他about ,每一个新<tr> Consider using a class-name about and a data-attribute with an id for each entry. 考虑为每个条目使用about的类名和带有ID的数据属性。 In the following example I have three rows with buttons for demonstration purposes 在下面的示例中,我有三行带有按钮的示例

 function getAboutForId(id) { let about = document.querySelector('.about[data-id="'+id+'"] input').value; alert(about); } 
 <div id="videos-div" class="videos"> <table class="stats" cellspacing="5"> <tr> <th class="hed" colspan="1">VideoName</td> <th class="hed" colspan="1">Date</td> <th class="hed" colspan="1">Description</td> </tr> <tr> <td>$videoname</td> <td>$date min</td> <td class="about" data-id="1"><input type="text" placeholder="$metadata2" /></td><td><input type="button" value="alert" onclick="getAboutForId(1)"></td> </tr><tr> <td>$videoname</td> <td>$date min</td> <td class="about" data-id="2"><input type="text" placeholder="$metadata2" /></td><td><input type="button" value="alert" onclick="getAboutForId(2)"></td> </tr><tr> <td>$videoname</td> <td>$date min</td> <td class="about" data-id="3"><input type="text" placeholder="$metadata2" /></td><td><input type="button" value="alert" onclick="getAboutForId(3)"></td> </tr> 

You can then get the input-value from the nested input of the td with this CSS-query: .about[data-id="1"] input . 然后,您可以使用以下CSS查询从td的嵌套输入中获取输入值: .about[data-id="1"] input It's best to use the value attribute with form-elements. 最好将value属性与form-elements一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM