简体   繁体   English

为什么这个jquery不起作用?

[英]Why won't this jquery work?

I want it so that when a person presses the enter button, it would execute my function. 我想要它,以便当一个人按下Enter键时,它将执行我的功能。

Here is the code: 这是代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js">
    $('textmoney').keydown(function (e){
    if(e.keyCode == 13){
        moneyFunction();
    }
})
    </script>

Why won't this work? 为什么不起作用?

You are missing either ID or class selector while attaching the event. 附加事件时,您缺少ID或类选择器。

If textmoney is class 如果textmoney是课程

$('.textmoney').keydown(function (e){
 if(e.keyCode == 13){
    moneyFunction();
}});

If textmoney is ID 如果textmoney是ID

$('#textmoney').keydown(function (e){
 if(e.keyCode == 13){
   moneyFunction();
}});

我认为您在jQuery选择器中缺少ID或类,因此取决于选择器,它应该像$('#textmoney')$('.textmoney')

As others stated, you are missing a ID or class selector before textmoney . 正如其他人所述,您在textmoney之前缺少ID或类选择器。 It should be #textmoney or .textmoney . 它应该是#textmoney.textmoney

But you are also inserting your script inside the script tag loading jQuery. 但是,您也将脚本插入到加载jQuery的脚本标签中。 You can't do that. 你不能那样做。 It is either you load a source or you run the code between the tag. 它是您加载源代码或在标记之间运行代码。 The src is always prioritized so your code will never be run. src总是被优先处理,因此您的代码将永远不会运行。

Separate the two, use 2 script tags : 分开两个,使用2个脚本标签:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    $('#textmoney').keydown(function (e){
        if(e.keyCode == 13){
            moneyFunction();
        }
    });
</script>

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

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