How to grab the value of the span element using jQuery

╄→гoц情女王★ 提交于 2019-12-26 11:39:32

问题


I have the following code:

<tr>
<td Width="50%" align="left">
<span id="ctl00_lblTotalDesc">Ext. Subtotal</span></td>
<td Width="50%" align="right">
<span id="ctl00_lblTotalValue">100,087,000.00</span></td>
</tr>

I used the following to grab the value of the 2nd span element:

spanValue = $('#ctl00_lblTotalValue').text();

But this doesn't seem to work in Spock/Geb. I get the following error:

TypeError: $(...).text is not a function

What am I doing wrong

I get the following error if I use, $('#ctl00_lblTotalValue')

[object HTMLTableElement]

Firefox console was not useful so used Chrome console.

In Chrome: if I try $('#ctl00_lblTotalValue'), I get

<span id="ctl00_lblTotalValue">100,087,000.00</span>

But .text() - gives Type error: Object # has no method 'text'


回答1:


Is "ctl00_lblTotalValue" the id you assigned to the span or is it a server-side control getting its ID auto-assigned by the environment?

For example, if I have this code in my .NET aspx page

<div id="pnlHeader" runat="server"></div>

it gets rendered in the html page as

<div id="ctl00_pnlHeader"></div>

If that is the case, I need to grab it in the jquery using this syntax

$("#<%= pnlHeader.ClientID%>").text()

If yours is a server-side control, you may need to grab the text using

spanValue = $("#<%= lblTotalValue.ClientID%>").text();



回答2:


It seems you have another js framework overriding the $ function. it means you have to use jQuery('...') instead. The other point you have to consider is, as far as I know in all js frameworks I've worked with, the $ function does the same thing, but sometimes it selects the first matched html element then when your output is like:

[object HTMLTableElement]

it means the html object is not a span element. try:

$('#ctl00_lblTotalValue').length

if the result was greater than 1, it mean you have more than one html element with this same id.




回答3:


Simply try this:

$("span", id:"ctl00_lblTotalValuee").text()

Assuming that the value ct100 is a constant and is not generated by your server side application randomly.



来源:https://stackoverflow.com/questions/20379189/how-to-grab-the-value-of-the-span-element-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!