简体   繁体   English

我如何获得 td 的值

[英]how can i get the value of a td

i want to get the value of this td我想得到这个td的值

my code我的代码

 function check() {
     var e = document.getElementById("ticket_category_clone");
     var str = e.options[e.selectedIndex].text;

     alert(str);
     if (str === "Hardware") {
         SPICEWORKS.utils.addStyle('#ticket_c_hardware_clone{display: none !important;}');
     }

 }

 SPICEWORKS.app.helpdesk.ready(check); 

and bang it does Nothing, what could be the problem?砰的一声,它什么也没做,可能是什么问题?

the html srry for not post it in the begging html 抱歉没有在乞讨中发布

 <select id="ticket_category_clone" name="ticket[category]" hdpp="ticket_category">
    <option value=""></option><option value="Hardware">Hardware</option>
    <option value="Software">Software</option>
    <option value="Rede" selected="selected">Rede</option>
    <option value="Pedidos">Pedidos</option>
    <option value="Formação/Dúvida">Formação/Dúvida</option>
    <option value="Outro">Outro</option><option value="#edit_categories#">Edit Categories...</option></select>

You could use你可以使用

var e = document.getElementById("td.body-c_hardware.cell-c_hardware");
var str = e.innerHTML;

To use getElementById , your tag needs an id要使用getElementById ,您的标签需要一个 ID

<table>
  <tr><td id="idValue">Content</td></tr>
</table>

and you need to fetch it by just using the id value你只需要使用 id 值来获取它

document.getElementById('idValue');

If you want to use CSS-like selectors, you may be best using a toolkit like jQuery如果你想使用类似 CSS 的选择器,你可能最好使用像 jQuery 这样的工具包

jQuery('#idValue');
jQuery('td.className');
jQuery('table.tableClass td.cellClass');

You need to get the select element by its id.您需要通过其 id 获取 select 元素。 You can then access either the text of the option or the value of the option.然后您可以访问选项的文本或选项的值。 I think you may want the value instead of the text.我认为您可能想要值而不是文本。 I have posted a solution for both ways.我已经发布了两种方式的解决方案。

http://jsfiddle.net/c4eWg/1 http://jsfiddle.net/c4eWg/1

var e = document.getElementById('ticket_category_clone');
alert(e.options[e.selectedIndex].innerHTML);

If you want the actual value, and not the text then use this instead.如果您想要实际值而不是文本,请改用它。 I suspect it might be what you want.我怀疑这可能是你想要的。

var e = document.getElementById('ticket_category_clone');
alert(e.value);

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

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