I have this function:
function validateVip(){
$("table[id=table-pss] tbody tr").each(function() {
var keval = $(this.cells[4]).find('input');
var data = $(keval[0]).val();
console.log("result: " + data);
console.log(data.includes([vip]));
var x;
if ((data.includes([vip])) == true)
{
x = "yay";
console.log("Vip Exists");
}
else
{
x = "nay";
console.log("No VIP");
}
return x;
});
}
Whenever I call it, it always return "undefined". Here is how I call the function validateVip
:
var isVip = validateVip();
console.log("is vip:" + isVip);
Can someone show me what I did wrong?
Thank you.
The value of x is returned by the callback passed to $().each
, which does nothing
You need to define the variable x
in the upper scope:
function validateVip() {
var x;
$("table[id=table-pss] tbody tr").each(function() {
var keval = $(this.cells[4]).find('input');
var data = $(keval[0]).val();
console.log("result: " + data);
console.log(data.includes([vip]));
if ((data.includes([vip])) == true) {
x = "yay";
console.log("Vip Exists");
} else {
x = "nay";
console.log("No VIP");
}
});
return x;
}
Note that x
will be equaled to the last item assignment, iterated using each
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.