简体   繁体   English

当我的代码添加到wordpress中时,为什么按键事件不起作用?

[英]Why is my key event not working when my code is added in wordpress?

The following code works flawlessly here, but on WP not so much. 下面的代码在这里可以正常工作,但是在WP上却没有那么多。 When I inspect the console there are no errors. 当我检查控制台时,没有错误。

$('input[name="tmp_post_tag"]').keypress(function(event) {

    var keycode = (event.keyCode ? event.keyCode : event.which);   
    if (keycode == '13'){ 
        event.preventDefault();
        alert('You pressed a "enter" key in textbox');
    }    
});  

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="">
    <input type="text" 
            id="cant_use" 
            name="tmp_post_tag" 
            value="" 
            data-taxonomy="post_tag" 
            data-taxtype="flat" 
            class="wpt-new-taxonomy-title js-wpt-new-taxonomy-title form-control wpt-form-textfield form-textfield textfield"
            data-wpt-type="textfield" 
            data-wpt-id="cant_use" 
            data-wpt- autocomplete="off">

</form>

What am I doing wrong? 我究竟做错了什么? FYI we are using input name because ID changes. 仅供参考,因为ID更改,我们正在使用输入名称。 Dont think thats the issue because it wirks here, 不要认为这是问题所在,因为它在这里存在问题,

可能是内容是动态生成的,请尝试在其中使用on函数和keypress事件。

$('input[name="tmp_post_tag"]').on('keypress',function(event) 

WordPress自己的jQuery接管了$变量,因此您需要改用jQuery ,即: jQuery('input[name="tmp_post_tag"]').keypress(function(event) {

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

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