我正在尝试进行简单的邮政编码搜索,用户输入邮政编码以查看他们是否有资格获得支持。我希望它是这样的,一旦他们推进输入就会显示结果,此刻我将它作为键盘。
由于它是邮政编码,它需要搜索空间以及空间以及大小写。我希望有人可以提供帮助。
您可以在此处查看代码: https://jsfiddle.net/esdrxq0n/3/
HTML
<h1>Enter your postcode to see if you qualify</h1>
<input type="text" id="postcode" value="" />
<div id="container" style="display:none;">Our services support your postcode</div>
<div id="sorry" style="display:none;">Sorry we do not operate in your area. Signup to be notified when we do.</div>
JAVA
$("#postcode").bind('keyup', function (e) {
if (e.which >= 97 && e.which <= 122) {
var newKey = e.which - 32;
// I have tried setting those
e.keyCode = newKey;
e.charCode = newKey;
}
$("#postcode").val(($("#postcode").val()).toUpperCase());
});
var validcode = ('SS94QL','SS9 4QL','SS143JA','SS14 3JA');
$("#postcode").keyup(function () {
var code = $(this).val();
if (code === validcode) {
$("#container").show();
$("#sorry").hide();
} else {
$("#container").hide();
$("#sorry").show();
}
});
答案 0 :(得分:2)
试试这个, 主要是你把预定义的代码放在一个数组中,然后你可以将类型化的字符串转换为大写,看看它是否存在于数组中
$("#postcode").bind('keyup', function (e) {
if (e.which >= 97 && e.which <= 122) {
var newKey = e.which - 32;
// I have tried setting those
e.keyCode = newKey;
e.charCode = newKey;
}
$("#postcode").val(($("#postcode").val()).toUpperCase());
});
var validcode = ['SS94QL','SS9 4QL','SS143JA','SS14 3JA'];
$("#postcode").keyup(function () {
var code = $(this).val().toUpperCase();
if (validcode.indexOf(code) > -1 ) {
$("#container").show();
$("#sorry").hide();
} else {
$("#container").hide();
$("#sorry").show();
}
});
&#13;
#postcode {
width:50%;
height:40px;
background-color:#ffffff;
margin:0px auto;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Enter your postcode to see if you qualify</h1>
<input type="text" id="postcode" value="" />
<div id="container" style="display:none;">Our services support your postcode</div>
<div id="sorry" style="display:none;">Sorry we do not operate in your area. Signup to be notified when we do.</div>
&#13;
答案 1 :(得分:0)
您的validCode
应该是一个数组,然后您可以使用indexOf
函数查找输入的代码是否存在于数组中
var validcode = ['SS94QL','SS9 4QL','SS143JA','SS14 3JA'];
$("#postcode").keyup(function () {
var code = $(this).val();
if (validcode.indexOf(code) > -1) {
$("#container").show();
$("#sorry").hide();
} else {
$("#container").hide();
$("#sorry").show();
}
});
<强> JSFIDDLE 强>