4

JSON ツリーを「検索」するための 3 つの入力フィールドがあります。3 つのフィールドすべてが入力され、正しい場合は、次の JSON レベルからデータを取得します。

JSON ツリーの次のデータを取得するために、keyup イベントで数値をカウントアップします。ただし、3 つのフィールドすべてが満たされるたびに、カウンターはリセットされます。

HTML

<h1>sein</h1>
<form>
    <input type="text" id=simple_present placeholder="simple present">
    <input type="text" id=simple_past placeholder="simple past">
    <input type="text" id=past_participle placeholder="past participle">
</form>
<div class="answer">Enter: be - was - been</div>

JS

$('input').on('keyup', function() {
    var count = 0;

    if (this.value === json.verbs.irregular[count++][this.id]) {
        $(this).prop('disabled', true).next().focus();
    }

    if ($('input:not(:disabled)').length === 0) {
        $('input').val('').prop('disabled', false).first().focus();
        $('h1').text(json.verbs.irregular[count++].infinitiv);
        alert(count);
    }
});

おそらく、キーイベントごとに変数が 0 に設定されるでしょうか? しかし、私はそれをkeyup-event の外に設定することはできません!

これは、私がこれまでに行ったことのデモンストレーションです。

次のように入力します。

  1. なれ
  2. だった
  3. その間

何か案は?

4

2 に答える 2

2

あなたはこれを試すことができます

var count = 0, amount = json.verbs.irregular.length-1, current = 0,
inputs = $("#simple_present, #simple_past, #past_participle");

$(inputs).on('keyup', function() {
    if (this.value === json.verbs.irregular[count][this.id]) {
        $(this).prop('disabled', true).next().focus();
    }

    if ($('input:not(:disabled)').length === 0) {
        $('input').val('').prop('disabled', false).first().focus();
        if(current < amount) { 
            current++; count++; 
        }
        else {
            current=0; count=0; 
        }
        $('h1').text(json.verbs.irregular[current].infinitiv);
    }
});​

デモ

于 2012-08-25T18:00:13.677 に答える
0

何かが働いている:

http://jsfiddle.net/fantactuka/56VBk/

于 2012-08-25T18:06:41.983 に答える