¿Puedo pasar dos parámetros junto con el evento onkeypress en javascript?

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

preguntado el 28 de mayo de 14 a las 11:05

Could you do a jsfiddle to show us your code in practice? -

Please consider using one of many templating techniques in preference to "stringing" together HTML. e.g. place in a dummy script bloquear con type="text/template" (so browsers ignore it), but you can then use string replace to insert values into placeholders (just a suggestion) :) -

Check your browser's console log for any errors. -

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 -

1 Respuestas

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 or haz tu propia pregunta.