gpt4 book ai didi

javascript - jQuery keyup 事件增加 -"function"将设置为 0

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:20:06 24 4
gpt4 key购买 nike

我有三个输入字段来“搜索”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到目前为止我所做的一切。

只需输入:

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

DEMO .

关于javascript - jQuery keyup 事件增加 -"function"将设置为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12124212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com