基于输入邮编的表单跳转实现需求及代码咨询
邮编对应预约表单跳转实现
我拥有一份服务区域的邮编列表,对应11个不同的预约表单URL,需要实现用户在输入框填写邮编后,自动跳转至对应的URL;若输入的邮编不在列表范围内,则跳转至通用联系表单。以下是我正在使用的代码:
JavaScript 逻辑代码
var location1 = ['97049', '97067', '97011']; var location2 = ['97055', '97023', '97022', '97009', '97089']; var location3 = ['97013', '97042', '97004', '97017', '97038']; var location4 = ['97019', '97014']; var location5 = ['97080', '97030', '97060', '97024', '97230']; var location6 = ['97222', '97267', '97015', '97086', '97045', '97027']; var location7 = ['97002', '97137', '97071', '97032']; var location8 = ['97114', '97127', '97115', '97132', '97111', '97148', '97128']; var location9 = ['97219', '97035', '97034', '97068', '97062', '97070', '97223', '97224', '97140']; var location10 = ['97204', '97205', '97209', '97201', '97210', '97221', '97239', '97231', '97229']; var location11 = ['98671', '98607', '98675', '98604', '98606', '98682', '98684', '98683', '98662']; $('#zipcodeSearch').submit(function(e) { e.preventDefault(); var root = location.protocol + '//' + location.host; var searchedZip = $('#zip-code').val(); if (jQuery.inArray(searchedZip, location1) > -1) { window.location.href = root + '/location1/'; } else if (jQuery.inArray(searchedZip, location2) > -1) { window.location.href = root + '/location2/'; } else if (jQuery.inArray(searchedZip, location3) > -1) { window.location.href = root + '/location3/'; } else if (jQuery.inArray(searchedZip, location4) > -1) { window.location.href = root + '/location4/'; } else if (jQuery.inArray(searchedZip, location5) > -1) { window.location.href = root + '/location5/'; } else if (jQuery.inArray(searchedZip, location6) > -1) { window.location.href = root + '/location6/'; } else if (jQuery.inArray(searchedZip, location7) > -1) { window.location.href = root + '/location7/'; } else if (jQuery.inArray(searchedZip, location8) > -1) { window.location.href = root + '/location8/'; } else if (jQuery.inArray(searchedZip, location9) > -1) { window.location.href = root + '/location9/'; } else if (jQuery.inArray(searchedZip, location10) > -1) { window.location.href = root + '/location10/'; } else if (jQuery.inArray(searchedZip, location11) > -1) { window.location.href = root + '/location11/'; } else { window.location.href = root + '/no-location/'; } });
HTML 页面代码
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form name="reg" onSubmit="return formValidation();"> <p>邮编: <input type="text" id="zipcodeSearch" pattern="\d{5}" maxlength="5" name="zip" required="required" /> </p> <input type="submit" id="submitbutton" name="submitbutton" value="提交" /> <div id="msg"></div> </form>
内容的提问来源于stack exchange,提问作者bibp
相关产品推荐
相关产品推荐

