如何从输入中获取值并将其传递给函数?

时间:2013-05-23 14:33:26

标签: javascript jquery html5 html input


我正在使用html5和jQuery制作小点击游戏。尽管我对这些人都很完整,但到目前为止我没有任何重大问题。但最近我遇到了一些我似乎无法解决的问题 - 我试图在网上搜索答案,但即使它们看似相关,我也无法解决它。事情就是这样:
我想输入玩家可以写出谜语的答案(并且最好通过按Enter键提交它,但现在我有按钮用于测试目的,因为onclick在我看来更容易做到这一点)并且答案是转到确定下一个对话框的功能。但无论如何,无论我做什么,输入似乎都没有进入这个功能。可能这是非常基本的错误,但我无法发现它。
这是代码:

<div id="answera" data-actor="francois">
        <input type="text" id='answer1'/>
        <input type="button" value="Test" onclick="testRiddle('answer1', '2')" />
</div>

testRiddle函数:

testRiddle: function(myfield, answer) {
    var myData = document.getElementById("#location_partI1 ."+myfield).value;
    if(myData!==''){
        if(myData == answer){
            game.dialogue.show($("#location_partI1 .answer1right"));

        }else{
            game.dialogue.show($("#location_partI1 .answer1wrong"));

        }
    }else{
        game.dialogue.show($("#location_partI1 .lackofinput"));

    }

}, 

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

看起来你需要:

testRiddle: function(myfield, answer) {
    var myData = document.getElementById(myfield).value;
    if(myData!==''){
        if(myData == answer){
            game.dialogue.show($("#location_partI1 .answer1right"));//is answer1right a class or id???

        }else{
            game.dialogue.show($("#location_partI1 .answer1wrong"));//same here???

        }
    }else{
        game.dialogue.show($("#location_partI1 .lackofinput"));//still the same here???

    }

}, 

答案 1 :(得分:0)

此:

var myData = document.getElementById("#location_partI1 ."+myfield).value;

应该是这样的:

var myData = $("#location_partI1 ."+myfield).val();
相关问题