简体   繁体   English

选择特定行时如何从行中的单元格获取值

[英]How to get value from a cell in a row when a certain row is selected

I created the html and javascript to get the id of a row that contains different information for example I'll create a table below. 我创建了html和javascript以获取包含不同信息的行的ID,例如,我将在下面创建一个表。

id-----firstname ---- lastname ------- product id -----名字----姓氏-------产品
1------Jon ---------------Doe ------------- Apple 1 ------乔恩--------------- Doe -------------苹果
2 ---- Jane ------------- Doe ------------- Banana 2 ----简-------------母鹿-------------香蕉

When I click on the row I want that rows id to be selected. 当我单击行时,我希望选择该行ID。 I have the following code 我有以下代码

alert(document.getElementById("id").value);

alert($(this).getElementbyId("id").value);//this line doesnt work

The first line will always come out to 1. Or the id that is in the first field. 第一行将始终显示为1。或第一字段中的ID。 The second line doesn't do anything. 第二行什么也没做。 Any help would be appreciated. 任何帮助,将不胜感激。

Thank you 谢谢


Some HTML that is outputted 输出的一些HTML

<table border=1 style="border-collapse: collapse">

<tr class='clickablerow'>
<td><textarea readonly cols='4' rows='1' id='id'>356</textarea></td> 
<td><textarea cols='20' rows='1' name='lastname'>Johnson</textarea></td> 
<td><textarea cols='20' rows='1' name='firstname'>Jon</textarea></td> 
<td><textarea cols='5' rows='1' name='room'>58B</textarea></td> 
<td><textarea cols='20' rows='1' name='product'>HP printer</textarea></td> 
<td><textarea cols='20' rows='1' name='problem'>lid is missing a part</textarea></td> 
<td><textarea cols='20' rows='1' name='finished'>5/16/2013</textarea></td> 
<td><textarea cols='20' rows='1' name='comments'>Fixed working now</textarea></td> 
</tr>
<tr class='clickablerow'><td><textarea readonly cols='4' rows='1' id='id'>429</textarea></td> 
<td><textarea cols='20' rows='1' name='lastname'>Harrison</textarea></td> 
<td><textarea cols='20' rows='1' name='firstname'>Wendy</textarea></td> 
<td><textarea cols='5' rows='1' name='room'>101</textarea></td> 
<td><textarea cols='20' rows='1' name='product'>Internet</textarea></td> 
<td><textarea cols='20' rows='1' name='problem'>Internet not working</textarea></td>
 <td><textarea cols='20' rows='1' name='finished'>5/24/2013</textarea></td> 
<td><textarea cols='20' rows='1' name='comments'>Need to change DNS</textarea></td>
 </tr>

Change the table row to something like this: 将表行更改为以下内容:

<tr class='clickablerow'><td><textarea readonly cols='4' rows='1' class='id'>".$row['id']."</textarea></td>...</tr>

Use class="id" instead of id="id" , because you can't repeat IDs. 使用class="id"而不是id="id" ,因为您不能重复ID。

Then your JS code can be: 然后您的JS代码可以是:

$("tr.clickablerow").click(function() {
    alert ($(this).find("textarea.id").val());
});

$(this).getElementById("id").value doesn't work because $(this) returns a jQuery object, not a DOM element. $(this).getElementById("id").value无效,因为$(this)返回jQuery对象,而不是DOM元素。 jQuery objects don't implement the same methods as DOM elements. jQuery对象没有实现与DOM元素相同的方法。

I am not sure if using jQuery is an option for you - but writing this down fairly quickly: 我不确定使用jQuery是否适合您-但可以很快写下来:

$("tr").click(function() {
     var currentID = $(this).attr("id");
     alert(currentID);
});

Explanation: $("tr").click() - you are binding the click event to each <tr> element on your page (if you don't want every tr element to be bound to this event, you can add a class name to those you want to have triggered and use $("tr.myClassName").click() ) 说明: $("tr").click() -您将click事件绑定到页面上的每个<tr>元素(如果您不希望将每个tr元素都绑定到该事件,则可以添加一个类命名为要触发的名称,并使用$("tr.myClassName").click()

$(this).attr("id") - $(this) refers to the element clicked. $(this).attr("id") -$(this)表示单击的元素。 .attr("id") gets the tr's attribute value (in this case the "id" of the html element). .attr(“ id”)获取tr的属性值(在本例中为html元素的“ id”)。

Hope that helps. 希望能有所帮助。

UPDATE: 更新:

I see that I have not understood the question clearly - thank you for bringing it to my attention - here is the amended sample: 我发现我对这个问题的理解不明确-感谢您引起我的注意-这是经过修改的示例:

$("tr").click(function() {
         var currentID = $("td textarea", this).val();
         alert(currentID);
});

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

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