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();
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744875604a4598528.html
评论列表(0条)