javascript-如何通过将用户输入与其键匹配来显示对象的值?

javascript-如何通过将用户输入与其键匹配来显示对象的值?,javascript,jquery,object,converter,Javascript,Jquery,Object,Converter,我想做一个转换工具,将一个代码(用户输入)转换成另一个(预定义的)。我决定使用Javascript对象作为代码的容器,我的函数将从Javascript对象中获取用户输入,实际上是一个键,将其与代码容器中的键匹配,如果找到匹配项,函数将向警报框显示值 我做了一个代码,但不起作用。我试图找到解决办法,但现在我失败了 这是我的密码: $(document).ready(function() { $("#convert").click(function(){ var Gardin

我想做一个转换工具,将一个代码(用户输入)转换成另一个(预定义的)。我决定使用Javascript对象作为代码的容器,我的函数将从Javascript对象中获取用户输入,实际上是一个键,将其与代码容器中的键匹配,如果找到匹配项,函数将向警报框显示值

我做了一个代码,但不起作用。我试图找到解决办法,但现在我失败了

这是我的密码:

$(document).ready(function() {
    $("#convert").click(function(){
        var GardinerToUnicodeCodePoint = {
                        "A1"    :"995328",
                        "A1A"   :"995329",
                        "A1B"   :"995330",
                        "A1C"   :"995331",
                        "A2"    :"995332",
                        "A2A"   :"995333",
                        "A3"    :"995334",
                        "A3A"   :"995335",
                        "A3B"   :"995336",
                        "A4"    :"995337",
                        "A4A"   :"995338",
                        "A4B"   :"995339",
                        "A4C"   :"995340",
                        "A4D"   :"995341",
                        "A4E"   :"995342",
                        "A5"    :"995343",
                        "A5A"   :"995344",
                        "A5B"   :"995345",
                        "A5C"   :"995346",
                        "A6"    :"995347",
        };
        var userInput = $("#userInput").val; /*for example 'A1'*/
        if (userInput in GardinerToUnicodeCodePoint) {
            alert(/*value of key 'userInput' -> 995328*/);
        } else {
            alert("No code found!");
        }
    });
});

调用对象后,可以使用
[]
获取键值对:

GardinerToUnicodeCodePoint[userInput]
将代码更改为:

    var userInput = $("#userInput").val; /*for example 'A1'*/
    if (userInput in GardinerToUnicodeCodePoint) {
        alert(GardinerToUnicodeCodePoint[userInput]);
    } else {
        alert("No code found!");
    }
参见JSFIDLE:

将用户输入的
字符串传递给该函数,它将返回您想要的内容

所以,您的完整解决方案如下所示:

$(document).ready(function() {
    $("#convert").click(function(){
        var userInput = $("#userInput").val(); /*for example 'A1'*/
        // a call to our new function keeping responsibilities seperated
        return getReturnCodeUsingKey(userInput);
    });
});

function getReturnCodeUsingKey(keyFromUserInput)
{
    var GardinerToUnicodeCodePoint = {
                    "A1"    :"995328",
                    "A1A"   :"995329",
                    "A1B"   :"995330",
                    "A1C"   :"995331",
                    "A2"    :"995332",
                    "A2A"   :"995333",
                    "A3"    :"995334",
                    "A3A"   :"995335",
                    "A3B"   :"995336",
                    "A4"    :"995337",
                    "A4A"   :"995338",
                    "A4B"   :"995339",
                    "A4C"   :"995340",
                    "A4D"   :"995341",
                    "A4E"   :"995342",
                    "A5"    :"995343",
                    "A5A"   :"995344",
                    "A5B"   :"995345",
                    "A5C"   :"995346",
                    "A6"    :"995347",
    };

    // set a variable to hold the return of the object query
    returnVal = GardinerToUnicodeCodePoint[keyFromUserInput];

    //return valid value from object, or string if undefined
    return returnVal ? returnVal : "error: no match found";
} 

这里的问题如注释中所述,您应该使用
$(“#userInput”).val()

代码示例:

$(“#转换”)。单击(函数(){
var GardinertoundoDecodePoint={
A1:'995328',
A1A:'995329',
A1B:'995330',
A1C:'995331',
A2:'995332',
A2A:‘995333’,
A3:‘995334’,
A3A:'995335',
A3B:'995336',
A4:‘995337’,
A4A:'995338',
A4B:'995339',
A4C:'995340',
A4D:'995341',
A4E:'995342',
A5:'995343',
A5A:'995344',
A5B:'995345',
A5C:'995346',
A6:'995347'
};
var userInput=$('#userInput').val();
var result=gardinertoincodecodepoint中的用户输入
?'userInput\'->'+userInput键的'Value'
:'未找到代码!';
控制台日志(结果);
});


提交
FYI,
val()
是一种方法。它需要括号,完全没有。现在它工作得很好。谢谢,你错过了我能帮你的机会。只要它在索引中的值是真实的,欢呼声就会响起。在示例代码中,这很好。但问题是缺少帕伦夫妇。
$(document).ready(function() {
    $("#convert").click(function(){
        var userInput = $("#userInput").val(); /*for example 'A1'*/
        // a call to our new function keeping responsibilities seperated
        return getReturnCodeUsingKey(userInput);
    });
});

function getReturnCodeUsingKey(keyFromUserInput)
{
    var GardinerToUnicodeCodePoint = {
                    "A1"    :"995328",
                    "A1A"   :"995329",
                    "A1B"   :"995330",
                    "A1C"   :"995331",
                    "A2"    :"995332",
                    "A2A"   :"995333",
                    "A3"    :"995334",
                    "A3A"   :"995335",
                    "A3B"   :"995336",
                    "A4"    :"995337",
                    "A4A"   :"995338",
                    "A4B"   :"995339",
                    "A4C"   :"995340",
                    "A4D"   :"995341",
                    "A4E"   :"995342",
                    "A5"    :"995343",
                    "A5A"   :"995344",
                    "A5B"   :"995345",
                    "A5C"   :"995346",
                    "A6"    :"995347",
    };

    // set a variable to hold the return of the object query
    returnVal = GardinerToUnicodeCodePoint[keyFromUserInput];

    //return valid value from object, or string if undefined
    return returnVal ? returnVal : "error: no match found";
}