英文:
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("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");
}
});
});
<!-- language: lang-html -->
<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>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论