在停在某个数字的循环中设置两个增量

Setting two increments in a loop that stops at a certain number

我有一个 jQuery 函数,我需要循环遍历两个变量,我需要递增到某个数字。这是函数:

var i = 1;
var x = 13;

$('input#question14_' + i).change(function() {
  if (this.checked) {
    $('.quiz_section.question-section-id-' + x).css('display', 'block')
  } else {
    $('.quiz_section.question-section-id-' + x).css('display', 'none')
  }
});

我需要 i15x28 然后停下来。显然我不想为每个数字都写出这样的函数,但我不太清楚如何递增这两个数字并循环遍历。

理想情况下,我会说尝试改用数据元素,这样您就可以拥有一个事件处理程序。这样每个元素都可以有一个 data-id="1" 和一个 data-other="13" 供您参考。但是,您可以从 id.

中提取并生成 x

//find all the elements who's id begins with question14_
$('input[id^="question14_"]').change(function() {
  //get the number after the _ and add 12
  var x = parseInt(this.id.split('_')[1], 10) + 12;
  
  if (this.checked) {
    $('.quiz_section.question-section-id-' + x).css('display', 'block')
  } else {
    $('.quiz_section.question-section-id-' + x).css('display', 'none')
  }
});