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;
    }

}

图片转代码服务由CSDN问答提供 功能建议

我试图在文本的onkeypress和onkeydown事件上调用函数validatQty。 当我刚刚将事件(单个参数)传递给此函数时,它工作正常。 但是,当我尝试添加第二个参数,我通过PHP获得,该函数不再被调用。 以下代码中的错误在哪里。

在我的PHP文件中

 <  code>&lt; input type =“text”id =“txtAnyName”
 onkeypress ='return validateQty(event,&lt;?php echo $ data ['anyName'];?&gt;)'
 onkeydown ='return validateQty  (事件,&lt;?php echo $ data ['anyName'];?&gt;)'&gt; 
   
 
 

Javascript文件 < / p>

  function validateQty(event,anyName)
 {
 alert(anyName); 
 
 var key = window.event?  event.keyCode:event.which; 
 
 if(key&lt; 48 || key&gt; 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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题