javascript - indexOf() function always return zero even have the same string -
i have variable in javascript
var hidden = "class_code,other";
then have ajax returning value
$.ajax({ type: "post", data: $("#myform").serialize, success: function(data){ if(hidden.indexof(data)){ //mycode here } } });
but doesn't work, try use alert()
print hidden.indexof(data)
, returns 0, try alert data , it's returning "class_code"
.
why script doesn't work hidden var contains data?
indexof
returns position matching string begins. since class_code
@ beginning of class_code,other
, 0
. when string isn't found, returns -1
. correct way test if string found with:
if (hidden.indexof(data) != -1)
Comments
Post a Comment