简体   繁体   English

获取表行中的元素

[英]Get An Element Within a Table Row

Firstly, If possible, I would like to do this without JQuery, and purely Javascript. 首先,如果可能的话,我想在没有JQuery和纯Javascript的情况下执行此操作。

Ok so I have an html table with rows getting added dynamically to it. 好的,所以我有一个html表,其中的行被动态添加到其中。

In each row is a: 在每一行中是:

  • Select Element (id = "ddFields") 选择元素(id =“ ddFields”)
  • Text Input Element (id = "tfValue") 文字输入元素(id =“ tfValue”)
  • Button Element (no id) 按钮元素(无ID)

The Button Element removes the row for which it is situated Button元素删除其所在的行

The Select Element has a default option of "" and then other 'valid' options 选择元素的默认选项为“”,然后其他“有效”选项

The Text Input is added to the row but it is hidden. 文本输入已添加到该行,但已隐藏。

All elements are in the same 所有元素都在同一位置

Basically I would like for the Select Element to show the hidden text input element if the selected index is != 0 基本上,如果选择的索引为!= 0,我希望Select元素显示隐藏的文本输入元素。

so far I have this for my onchange function: 到目前为止,我的onchange函数具有以下功能:

function itemChanged(dropdown) //called from itemChanged(this)
{ 
   var cell         = dropdown.parentNode;
   var row      = cell.parentNode;
   var rowIndex = dropdown.parentNode.parentNode.rowIndex;
   var index            = dropdown.selectedIndex;
   var option           = dropdown.options[dropdown.selectedIndex].text;

   if(index >0)
   {    
     alert(row);
     var obj=row.getElementById("tfValue"); //tfValue is the Text Field element
     alert(obj);
     //row.getElementById("tfValue").hidden = "false"; //doesn't work
     //row.getElementById("tfValue").setAttribute("hidden","true"); //doesn't work
   }
   else
   {
      alert('none selected');

   }
}

Finally figured it out. 终于想通了。 So here it is: 所以这里是:

SCENARIO: A table that has rows added to it dynamically. 场景:一个表,其中动态添加了行。

In each row there is a select element and an input text element. 每行中都有一个选择元素和一个输入文本元素。

The select element changes text-value of the input text element based on the index of the select element. 选择元素根据选择元素的索引更改输入文本元素的文本值。

in your select element set the onchange function to this: 在选择元素中,将onchange函数设置为此:

onchange="selectionChanged(this)"

then create a javascript function shown below: 然后创建如下所示的javascript函数:

function selectionChanged(dropdown)
{
   //get the currently selected index of the dropdown
   var index     = dropdown.selectedIndex;

   //get the cell in which your dropdown is
   var cell      = dropdown.parentNode;located

   //get the row of that cell
   var row       = cell.parentNode;

   //get the array of all cells in that row 
   var cells     = row.getElementsByTagName("td");

   //my text-field is in the second cell
   //get the first input element in the second cell
   var textfield = cells[1].getElementsByTagName("input")[0]; 

   //i use the first option of the dropdown as a default option with no value
   if(index > 0)  
   {
      textfield.value = "anything-you-want";
   }
   else
   {
      textfield.value = null;
   }
}

Hope this helps whoever. 希望这对任何人有帮助。 It bugged me for a very long time. 它困扰了我很长时间。 Thanks shub for the help! 感谢shub的帮助! :) :)

first, I hope you are not repeating your ids. 首先,希望您不要重复输入ID。 No two items should have the same id. 没有两个项目应具有相同的ID。

If you're iterating, create id1, id2, id3. 如果要迭代,请创建id1,id2,id3。

Also, this is not necessary but I suggest introducing your vars like this: 另外,这不是必需的,但是我建议像这样介绍您的var:

var a = x, 
    b = y,
    c = z;

If you decide to use jQuery you could just do this: 如果您决定使用jQuery,则可以执行以下操作:

$('#tableid').on('click','button',function(){
    $(this).parent().parent().remove();
});

$('#tableid').on('change', 'select',function(){
    if($(this).val()){ $(this).next().show(); }
});

Be sure to change #tableid to match your table's id. 确保更改#tableid以匹配表的ID。

This assumes the text input is the very next element after the select box. 假定文本输入是选择框之后的下一个元素。 If not so, adjust as necessary or ask and I'll edit. 如果不是这样,请根据需要进行调整或询问,然后我会进行编辑。

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

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