比较jquery val VS数组值

时间:2012-09-19 04:02:08

标签: jquery

我需要比较2个值。一个值来自输入框,另一个值来自数组。

    var caja = $("#txtRespuesta").val();
    alert("Comprueba respuesta: " + caja);
    $("#puntos").text(puntos); 
    var obj2String = (arregloDePhp[indiceArregloPhp]).toString();
    // if (res2.equals(obj2String.toString())){ //complains that object has no method .equals
     if (caja==obj2String){ 
    puntos+=1; 
    };
    console.log ("comparing: "+caja+" y "+obj2String);//actually this shows both words are the same
    indiceArregloPhp+=1;

找不到它是什么,请指出我正确的方向。

提前致谢。

2 个答案:

答案 0 :(得分:0)

您可以使用caja===obj2String 另外,如果你在使用$ .trim()进行比较之前修剪该值会更好。

答案 1 :(得分:0)

您可以使用$.inArray() http://api.jquery.com/jQuery.inArray/

$.inArray(valueFromInput, yourArray) // returns the index or -1 if not found
相关问题