jQuery keyup事件增加-“;函数“;将设置为0

jQuery keyup event increase-"function" will set to 0

本文关键字:设置 函数 keyup 增加 jQuery 事件      更新时间:2023-09-26

我有三个输入字段来"搜索"JSON树。如果所有三个字段都已填充并正确,则从下一个JSON级别获取数据。

我通过keyup事件向上计数,以获得JSON树的下一个数据。但每当所有三个字段都被填满时,计数器就会重新设置种子。

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="answerswer">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-事件之外设置它!

这是我迄今为止所做的一个示范。

仅类型:

  1. be
  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);
    }
});​

演示

有一些工作:

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