简体   繁体   English

JSFiddle中未执行的jQuery代码

[英]jQuery code not executed in JSFiddle

I am still learning jQuery and I referred to some tutorials online. 我仍在学习jQuery,并且在线参考了一些教程。 I got my code from this site and i plugged it into JSFiddle to try but it didn`t work: 我从此站点获得了代码,然后将其插入JSFiddle进行尝试,但没有成功:

http://www.leniel.net/2012/10/javascript-regex-jquery-to-match-only-english-characters-in-input-textbox.html#sthash.XQxbhteA.1FTBwPAJ.dpbs http://www.leniel.net/2012/10/javascript-regex-jquery-to-match-only-english-characters-in-input-textbox.html#sthash.XQxbhteA.1FTBwPAJ.dpbs

HTML 的HTML

<input id="mytextbox" style="width:300px" placeholder="Only English letters are allowed here...">

JS JS

$("#mytextbox").on("keypress", function(event) {

    // Disallow anything not matching the regex pattern (A to Z uppercase, a to z lowercase and white space)
    // For more on JavaScript Regular Expressions, look here: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
    var englishAlphabetAndWhiteSpace = /[A-Za-z ]/g;

    // Retrieving the key from the char code passed in event.which
    // For more info on even.which, look here: http://stackoverflow.com/q/3050984/114029
    var key = String.fromCharCode(event.which);

    //alert(event.keyCode);

    // For the keyCodes, look here: http://stackoverflow.com/a/3781360/114029
    // keyCode == 8  is backspace
    // keyCode == 37 is left arrow
    // keyCode == 39 is right arrow
    // englishAlphabetAndWhiteSpace.test(key) does the matching, that is, test the key just typed against the regex pattern
    if (event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 || englishAlphabetAndWhiteSpace.test(key)) {
        return true;
    }

    // If we got this far, just return false because a disallowed key was typed.
    return false;
});

$('#mytextbox').on("paste",function(e)
{
    e.preventDefault();
});

The framework I used was No-Library (pure JS) and extension was set to onload . 我使用的框架是No-Library(纯JS) ,扩展名设置为onload

This script however seems to work on the site I referred to. 但是,该脚本似乎可以在我提到的网站上运行。 May I know where I did wrong? 我可以知道我做错了什么吗? Would it be that I did not include some jQuery libraries? 难道我没有包括一些jQuery库吗?

As a framework, you need to select a jQuery version (eg jQuery 2.1.0 ) instead of No-Library (pure JS) . 作为框架,您需要选择jQuery版本(例如jQuery 2.1.0 )而不是No-Library(纯JS)

If you don't, the jQuery library will not be loaded and hence your jQuery-dependent code will not work. 如果您不这样做,则不会加载jQuery库,因此与jQuery相关的代码将无法工作。

You must include the jquery files and use it 您必须包括jquery文件并使用它

 $("#mytextbox").on("keypress", function(event) { // Disallow anything not matching the regex pattern (A to Z uppercase, a to z lowercase and white space) // For more on JavaScript Regular Expressions, look here: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions var englishAlphabetAndWhiteSpace = /[A-Za-z ]/g; // Retrieving the key from the char code passed in event.which // For more info on even.which, look here: http://stackoverflow.com/q/3050984/114029 var key = String.fromCharCode(event.which); //alert(event.keyCode); // For the keyCodes, look here: http://stackoverflow.com/a/3781360/114029 // keyCode == 8 is backspace // keyCode == 37 is left arrow // keyCode == 39 is right arrow // englishAlphabetAndWhiteSpace.test(key) does the matching, that is, test the key just typed against the regex pattern if (event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 || englishAlphabetAndWhiteSpace.test(key)) { return true; } // If we got this far, just return false because a disallowed key was typed. return false; }); $('#mytextbox').on("paste",function(e) { e.preventDefault(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="mytextbox" style="width:300px" placeholder="Only English letters are allowed here..."> 

Select a jQuery library in the top-left dropdown and it should work ok as per my Fiddle below: 在左上角的下拉列表中选择一个jQuery库,按照我下面的小提琴,它应该可以正常运行:

http://jsfiddle.net/Delorian/8b7trd6d/ http://jsfiddle.net/Delorian/8b7trd6d/

(copied OP's code)

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

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