我想构建一些可以让用户输入的内容,例如段落编号,例如5,然后在他们单击按钮时生成该段落数?

我猜我可以将一个段落存储在一个没有问题的变量中,但不确定如何从用户输入中将其乘以:)

如果我得到一些帮助,并开始解释如何做,我将不胜感激!

var $paragraph = $(
 '<p> Tired of lorum Ipsum? why not try puff-puffing ipsum? A simple and fun
  generator to help all those colour puffs that are in need. Put some puff into
  your project.</p>'
});

最佳答案

抓住用户的值并使用for循环生成输出,然后将其放在页面上。



$("form").on("submit", function(e) {
  e.preventDefault();
  var num = $('#num').val(),
    paragraph =
      "<p>Tired of lorum Ipsum? why not try puff-puffing ipsum? A simple and fun generator to help all those colour puffs that are in need. Put some puff into your project.</p>",
      output = '';
  for (var i = 0; i < num; i++) {
    output += paragraph;
  }
  $('#output').html(output);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="number" id="num">
<input type="submit" value="generate">
</form>

<div id="output"></div>

关于javascript - jQuery输入段落生成器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44092352/

10-16 13:40