admin管理员组文章数量:1404927
I have tested every solution I have found on Internet, but none of them works.
I have this HTML:
<h4>Códigos disponibles:<span id="codesQuantity">@Model.ExternalCodesForThisProduct</span></h4>
And this Javascript:
$('#eCodesFrm').on('submit', function (e) { //use on if jQuery 1.7+
e.preventDefault(); //prevent form from submitting
var availableCodes = $("#codesQuantity");
var totalCodesUsed = 0;
alert(availableCodes);
$('#eCodesFrm *').filter(':input').each(function () {
if (this.name.match(/.Quantity$/)) {
totalCodesUsed = totalCodesUsed + parseInt(this.value, 10);
}
});
But availableCodes
is [object Object]
.
What am I doing wrong?
I have tested every solution I have found on Internet, but none of them works.
I have this HTML:
<h4>Códigos disponibles:<span id="codesQuantity">@Model.ExternalCodesForThisProduct</span></h4>
And this Javascript:
$('#eCodesFrm').on('submit', function (e) { //use on if jQuery 1.7+
e.preventDefault(); //prevent form from submitting
var availableCodes = $("#codesQuantity");
var totalCodesUsed = 0;
alert(availableCodes);
$('#eCodesFrm *').filter(':input').each(function () {
if (this.name.match(/.Quantity$/)) {
totalCodesUsed = totalCodesUsed + parseInt(this.value, 10);
}
});
But availableCodes
is [object Object]
.
What am I doing wrong?
Share Improve this question edited Aug 24, 2023 at 19:01 isherwood 61.2k16 gold badges121 silver badges170 bronze badges asked Apr 15, 2015 at 11:16 VansFannelVansFannel 46.1k118 gold badges378 silver badges653 bronze badges 6-
try
var availableCodes = jQuery("#codesQuantity");
– Arun P Johny Commented Apr 15, 2015 at 11:17 -
1
if
$
is referring to jQuery object... even if the element is not availableundefined
won't be returned.. an empty jQuery object will be retunred – Arun P Johny Commented Apr 15, 2015 at 11:22 -
1
so since you are getting
undefined
... it looks like$
is no longer referring to jQuery – Arun P Johny Commented Apr 15, 2015 at 11:22 -
3
@VansFannel Hey you initially told us like, you are receiving
undefined
??? But now you changed it.. Follow shaunak's suggestion – Rajaprabhu Aravindasamy Commented Apr 15, 2015 at 11:23 -
1
Best way to
help your self
is to useconsole.log(availableCodes)
instead ofalert(availableCodes)
, it says almost everything. – Mox Shah Commented Apr 15, 2015 at 11:25
3 Answers
Reset to default 2If you need the inner element try .html()
. As long as it's plain text in there there shouldn't be a problem.
To get the text inside the <span>
use .text()
:
jQuery("#codesQuantity").text() //or $("#codesQuantity").text() if $ is jQuery in your code.
The problem here is that you're assigning a jQuery object to your variable, and not the content of the element. To extract the text inside the <span>
, you should use either .html()
or .text()
, and do this instead:
var availableCodes = $("#codesQuantity").text();
本文标签: javascriptHow can I get the value of a span element with jQueryStack Overflow
版权声明:本文标题:javascript - How can I get the value of a span element with jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744875604a2629896.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论