我有三个输入字段来“搜索”JSON树。如果所有三个字段都已填充并且正确,则从下一个JSON级别获取数据。
我通过keyup-event计算一个数字来获取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="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
- 事件之外!
这是迄今为止我所做的demonstration。
只需输入:
有什么想法吗?
答案 0 :(得分: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);
}
});
答案 1 :(得分:0)