简体   繁体   English

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

[英]Javascript function is not getting called on OnKeyPress and OnKeyDown event

I am trying to call a function validatQty on onkeypress and onkeydown event of a text. 我正在尝试在文本的onkeypress和onkeydown事件上调用validatQty函数。 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. 但是,当我尝试添加通过PHP获取的第二个参数时,该函数不再被调用。 Where is the error in my following code. 我的以下代码中的错误在哪里。

In my PHP File 在我的PHP文件中

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

Javascript file Javascript文件

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

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

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

}

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 这是jsbin的工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM