【问题标题】:Javascript while loop inside .after().after() 中的 Javascript while 循环
【发布时间】:2013-07-12 03:12:06
【问题描述】:

例如,是否可以在 .after() 中编写 while 循环

$(".item-row:last").after('<tr class="item-row"><td class="description"><select name="description">' + while(x<2){ + '<option>' + variable + '</option> '+ x++; } + '</select></td></tr>');

【问题讨论】:

  • 没有。看起来您可能想要在 .after() 之外构建字符串,然后传入构建的字符串。
  • 也许你已经在我的回答中发现了错误,但我更新了它以添加缺少的 } 并确保“x”增加。

标签: javascript jquery while-loop


【解决方案1】:

不是这样,但你总是可以使用函数:

$(".item-row:last").after(function() {
  var markup = '<tr class="item-row"><td class="description"><select name="description">';
  while (x < 2) {
    markup += '<option>' + variable + '</option>';
    x++;
  }
  markup += '</select></td></tr>';
  return markup;
}());

请注意,“.after()”参数列表中的函数被调用,因此“.after()”被传递给调用该匿名函数的结果 .

【讨论】:

    猜你喜欢
    • 2014-12-14
    • 2018-02-19
    • 2011-03-31
    • 2015-09-18
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    相关资源
    最近更新 更多