j.Query.inArray在外部列表上无法正常工作,但在普通数组上可以正常工作。

huangapple go评论58阅读模式
英文:

j.Query.inArray not working with external list but working with normala array

问题

I am working on a website that only allows sign-ups from business email addresses, so I have to check if the entered email is a free email and restrict registration.

My problem is when I manually enter the domains into my array, everything works fine, but when I load the domains from a text file into the array, it doesn't work.

英文:

I am working on a website that only allow sign up from business email so i have to check if the entered email is a free email and restrict registration.

My problem is when i enter the domains manually in my array, everything works fine but when i load the domains from a txt file in to the array, it doesn't work.

Please can anybody help. I have tried everything including reducing the domains in the list but still nothing works

<script type="text/javascript">
    $(document).ready(function () {
      var blocked = new Array;
      $.get("https://cdn.jsdelivr.net/gh/cokeboyyy/blackout@main/blocked.txt", function (data) {
        var blocked = data.split("\n");
        console.log(blocked);
      });

      $("button").click(function () {
        var domain = $("input").val();
        if (jQuery.inArray(domain, blocked) !== -1) {
          $("#check").html("available");
        } else {
          $("#check").html("not available");
        }
      });
    });
  </script>

答案1

得分: 0

只需删除get函数中的var

$(document).ready(function() {
  var blocked = new Array;
  $.get("https://cdn.jsdelivr.net/gh/cokeboyyy/blackout@main/blocked.txt", function(data) {
    blocked = data.split("\n");
  });
  $("button").click(function() {
    var domain = $("input").val();
    if (jQuery.inArray(domain, blocked) !== -1) {
      $("#check").html("available");
    } else {
      $("#check").html("not available");
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="input">
<button>check</button>
<div id="check"></div>
英文:

Just delete the var in get function.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function() {
  var blocked = new Array;
  $.get(&quot;https://cdn.jsdelivr.net/gh/cokeboyyy/blackout@main/blocked.txt&quot;, function(data) {
    blocked = data.split(&quot;\n&quot;);
  });
  $(&quot;button&quot;).click(function() {
    var domain = $(&quot;input&quot;).val();
    if (jQuery.inArray(domain, blocked) !== -1) {
      $(&quot;#check&quot;).html(&quot;available&quot;);
    } else {
      $(&quot;#check&quot;).html(&quot;not available&quot;);
    }
  });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;input type=&quot;text&quot; id=&quot;input&quot;&gt;
&lt;button&gt;check&lt;/button&gt;
&lt;div id=&quot;check&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月15日 08:38:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定