if语句回显不同代码的不同链接

我正在使用此代码

HTML:

some message

jQuery的:

 $(function() { $("#message").hide(); $("#fail").hide(); var prefix = ['HX', 'HD', 'BD', 'LS'] $('#myInput').keyup(function(e) { var value = $(this).val(); var firstTwo = value.substr(0,2); var firstTwoUpper = firstTwo.toUpperCase(); if(firstTwo != firstTwoUpper) { $(this).val( value.replace(/^\w\w/, firstTwoUpper) ); } if(value.length > 1) { if($.inArray(firstTwoUpper, prefix) >= 0) { $("#fail").hide(); $("#message:hidden").fadeIn(); } else { $("#message").hide(); $("#fail:hidden").fadeIn(); } } else { $("#message").hide(); $("#fail").hide(); } }); }); 

要成为邮政编码检查。 我从这里得到了上面的代码: Jquery检查数组执行函数 ,它工作得很好但我想显示一条消息,其中包含一个彼此不同的链接。 例如,我希望代码’HX’链接到Facebook,我希望’BD’链接到BBC。 我如何正确编写if语句? 我尝试了各种各样的方法,但我没有达到我的需要。

有人可以给我一点指导吗?

谢谢,

本。

这个小提琴做了你可能正在寻找的东西

  
Invalid postcode
$(function () { $("#message").hide(); $("#fail").hide(); var prefix = ['HX', 'HD', 'BD', 'LS']; var links = ['http://www.facebook.com','http://www.example.com','http://www.bbc.com','#']; $('#myInput').keyup(function (e) { var value = $(this).val(); var firstTwo = value.substr(0, 2); var firstTwoUpper = firstTwo.toUpperCase(); if (firstTwo != firstTwoUpper) { $(this).val(value.replace(/^\w\w/, firstTwoUpper)); } if (value.length > 1) { var index = $.inArray(firstTwoUpper, prefix); if (index >= 0) { $("#fail").hide(); $("#message").fadeIn(); $("#message a").attr("href",links[index]).html(links[index]) } else { $("#message").hide(); $("#fail:hidden").fadeIn(); } } else { $("#message").hide(); $("#fail").hide(); } }); });

这个小提琴: http : //jsfiddle.net/um67M/2/

我将前缀从字符串数组更改为对象数组。

如果由于fadeIn()的速度用户输入的速度非常快,您将会看到一个问题。

 $(function() { $("#message").hide(); $("#fail").hide(); var prefix = [{code:'HX',link:'www.facebook.com'}, {code:'BD',link:'www.bbc.com'}]; $('#myInput').keyup(function(e) { var value = $(this).val(); var firstTwo = value.substr(0,2); var firstTwoUpper = firstTwo.toUpperCase(); if(firstTwo != firstTwoUpper) { $(this).val( value.replace(/^\w\w/, firstTwoUpper) ); } if(value.length > 1) { for(var obj in prefix){ console.log(firstTwoUpper, prefix[obj].code, firstTwoUpper == prefix[obj].code); if(firstTwoUpper === prefix[obj].code){ $("#fail").hide(); var link = 'https://stackoverflow.com/questions/24781076/if-statement-echo-out-different-links-for-different-codes/'+prefix[obj].link+''; $("#message").append(link); $("#message:hidden").fadeIn(); break; }else { $("#message").hide(); $("#fail:hidden").fadeIn(); } }; } else { $("#message").empty(); $("#message").hide(); $("#fail").hide(); } }); }); 
 $('#myInput').keyup(function (e) { var value = $(this).val(); var firstTwo = value.substr(0, 2); var firstTwoUpper = firstTwo.toUpperCase(); if (firstTwo != firstTwoUpper) { $(this).val(value.replace(/^\w\w/, firstTwoUpper)); } if (value.length > 1) { if ($.inArray(firstTwoUpper, prefix) >= 0) { if (firstTwoUpper == "HX") { $("#message").empty(); $("#message").html('

some message

Google'); } else if (firstTwoUpper == "HD") { $("#message").empty(); $("#message").html('

other message

Stack OverFlow'); } $("#fail").hide(); $("#message:hidden").fadeIn(); } else { $("#message").hide(); $("#fail:hidden").fadeIn(); } } else { $("#message").hide(); $("#fail").hide(); } });

小提琴在这里小提琴