How can you check to see whether the returned results contain a specific value?
$(function () {
$('form').submit(function (e) {
e.preventDefault();
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
//here i wanna check if the result return contains value "test"
//i tried the following..
if($(result).contains("test")){
//do something but this doesn't seem to work ...
}
}
},
});
});
});
Array.prototype.indexOf()
Since this still gets up votes I’ll edit in a more modern answer.
With es6 we can now use some more advanced array methods.
Array.prototype.includes()
result.includes("test")which will return a true or false.Array.prototype.some()
If your array contains objects instead of string you can use
result.some(r => r.name === 'test')which will return true if an object in the array has the name test.