Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查数组中的字符是否存在于错误的字符串中_Javascript_Jquery - Fatal编程技术网

Javascript 检查数组中的字符是否存在于错误的字符串中

Javascript 检查数组中的字符是否存在于错误的字符串中,javascript,jquery,Javascript,Jquery,我有一个输入html元素,我在jquery中使用模糊函数检查该值是否包含特定元素,但我的代码不起作用 jQuery( "#email" ).blur(function() { ... var arr = ["1","6","7"]; if(this.value()){ // check if the string value contains characters in arr Array .. 有什么想法吗? 我还尝试使用indexOf(),但仍然不起作用 } …您可以使用和的组合 希望这

我有一个输入html元素,我在jquery中使用模糊函数检查该值是否包含特定元素,但我的代码不起作用

jQuery( "#email" ).blur(function() {
...
var arr = ["1","6","7"];
if(this.value()){ // check if the string value contains characters in arr Array
..
有什么想法吗? 我还尝试使用
indexOf()
,但仍然不起作用

}


您可以使用和的组合


希望这段代码有用

// Defining array of predefined chars outside the blur event.
// No need to define it on every blur event.
var arr = ["1","6","7"]; //array of predefined chars.
$( "#email" ).blur(function(event) { // Expecting an input element
var _getText = $(this).val();  // get the value from input
for(var i = 0;i<arr.length;i++){  // loop through array of predefined chars
  // use indexOf to check if input val 
  //contain any of predefined chars
  if(_getText.indexOf(arr[i]) ==-1){  
   console.log(arr[i] + " is not present in the string");
  }
  else{
   console.log(arr[i]+ " is present in the string");
  }
}
})
//定义模糊事件外部预定义字符的数组。
//无需在每个模糊事件上定义它。
var arr=[“1”、“6”、“7”]//预定义字符的数组。
$(“#email”).blur(函数(事件){//需要输入元素
var _getText=$(this).val();//从输入中获取值

对于(var i=0;i

我有一种不同的方法,我希望这会有很大帮助:

(function($) {
    var arr = ["1","6","7"];

    $(document).ready(function() {
        var email = document.getElementById("email");
        var lastCharacter;

        email.addEventListener("input", function(e) {
            lastCharacter = this.value.substr(-1);

            for (var i = 0; i < arr.length; i++) {
                if (arr[i] === lastCharacter) {
                    console.log("Exist - Contains an specific element of the Arr");
                }
            }

        }, false);

    });

})(jQuery);
(函数($){
var arr=[“1”、“6”、“7”];
$(文档).ready(函数(){
var email=document.getElementById(“电子邮件”);
变异性;
email.addEventListener(“输入”,函数(e){
lastCharacter=this.value.substr(-1);
对于(变量i=0;i
你是如何使用
indexOf
this.value()
是错误的,它应该是
this.value
因为你想逐字符检查,你可能必须使用
keypress
即使我不能使用keypress,一些安卓设备也无法识别它
(function($) {
    var arr = ["1","6","7"];

    $(document).ready(function() {
        var email = document.getElementById("email");
        var lastCharacter;

        email.addEventListener("input", function(e) {
            lastCharacter = this.value.substr(-1);

            for (var i = 0; i < arr.length; i++) {
                if (arr[i] === lastCharacter) {
                    console.log("Exist - Contains an specific element of the Arr");
                }
            }

        }, false);

    });

})(jQuery);