duanjiaolia97750 2014-03-28 10:58
浏览 78
已采纳

在OnKeyPress和OnKeyDown事件上没有调用Javascript函数

I am trying to call a function validatQty on onkeypress and onkeydown event of a text. It was working fine when I was just passing event (single parameter) to this function. But when I tried to add a second parameter which I am getting through PHP, the function is not getting called anymore. Where is the error in my following code.

In my PHP File

<input type="text" id="txtAnyName" 
       onkeypress='return validateQty(event, <?php echo $data['anyName']; ?>)' 
       onkeydown='return validateQty(event, <?php echo $data['anyName']; ?>)'>

Javascript file

function validateQty(event, anyName) 
{   
    alert(anyName);

    var key = window.event ? event.keyCode : event.which;

    if ( key < 48 || key > 57 ) 
    {
        return false;
    }

}
  • 写回答

1条回答 默认 最新

  • duanmiao6695 2014-03-28 11:07
    关注

    you missing double quotes in your function...

    <input type="text" id="txtAnyName" 
           onkeypress='return validateQty(event, "<?php echo $data['anyName']; ?>")' 
           onkeydown='return validateQty(event, "<?php echo $data['anyName']; ?>")'>
    

    here is working example of jsbin

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?