if语句回声出不同的codeS不同的链接 [英] if statement echo out different links for different codes

查看:98
本文介绍了if语句回声出不同的codeS不同的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这code

HTML

<div id="message">some message</div>
<input type="text" id="myInput" />​    

jQuery的:

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();
    }
 });
});


要成为一个岗位code检。我得到了上述code从这里:<一href=\"http://stackoverflow.com/questions/2896418/jquery-check-array-perform-function/24780023#24780023\">Jquery检查数组执行功能以及它工作得很好,但我想显示包含一个链接,都彼此不同的消息。例如,我想codeHX链接到Facebook和我想要'BD'链接到BBC。我将如何正确地写if语句?我尝试了各种方法,但我没有达到我所需要的。

​ To be a post code check. I got the above code from here: Jquery check array perform function and it works just fine but I want to display a message containing a link which all differ from one another. For example I want code 'HX' to link to facebook and I want 'BD' to link to the BBC. How would I write the if statement correctly? I have tried various ways but I am not achieving what I need.

可能有人给我一点点guideance好吗?

Could someone give me a little guideance please?

谢谢,

本。

推荐答案

小提琴做什么你可能会寻找

<div id="message"><a href="#">Some link</a></div>
<div id="fail">Invalid postcode</div>
<input type="text" id="myInput" />

$(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();
        }
    });
});

这篇关于if语句回声出不同的codeS不同的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆