¿Puedo pasar dos parámetros junto con el evento onkeypress en javascript?
Frecuentes
Visto 1,022 equipos
0
Can I pass two parameters with the onkeypress
event in JavaScript?
Here I passed onkeypress = "return isNumberKey(event,'+i+')"
I want validate it as number also use that number for future use.
for(var i = 0; i < (parsed_data.length - 1); i++) {
que = que + '<tr>';
que = que + '<td align="center" style="text-align: center;" ><input type="text" id="itemquentity[' + i + ']" value="' + parsed_data[i].itemquantity + '" onkeypress="return isNumberKey(event,' + i + ')" size="2" maxlength="2"></td>';
que = que + '<td align="center" style="text-align: center;" ><label id="productitemprice[' + i + ']" for="productitemprice">' + parsed_data[i].productitemprice + '</label>$</td>';
que = que + '<td align="center" style="text-align: center;" >' + parsed_data[i].productitemimgpath + '</td>';
que = que + '</tr>';
totalproductitemprice = totalproductitemprice + parseInt(parsed_data[i].productitemprice);
}
function isNumberKey(evt ,i ) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
alert('#productitemprice['+i+']');
return false;
}
return true;
}
But when I do this it neither giving me alert message nor doing validation
1 Respuestas
0
if you need numbers only allowed in text box try this
onkeyup="this.value=this.value.replace(/[^0-9 ]/g,'')";
contestado el 28 de mayo de 14 a las 12:05
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas javascript jquery or haz tu propia pregunta.
Could you do a jsfiddle to show us your code in practice? - scragar
Please consider using one of many templating techniques in preference to "stringing" together HTML. e.g. place in a dummy
script
bloquear contype="text/template"
(so browsers ignore it), but you can then use string replace to insert values into placeholders (just a suggestion) :) - iCollect.it LtdCheck your browser's console log for any errors. - Kayla
instead of sending i and then find an object you could send this object i.e. return isNumberKey(event,this). Then you can access that specific product item quantity object. To know more about your problem we need to get more details. Otherwise, use dev tools to find js error - codebased