简体   繁体   English

如何显示相同的div次数是用户在jquery中输入的次数?

[英]How do I display a same div for a number times that user input in jquery?

I have POS system with cheque payments. 我有带有支票付款的POS系统。 when the cashier input the number of cheque according to the input the cheque detail form should generated. 当收银员根据输入内容输入支票号码时,应生成支票明细表。

Eg : if he enter Number 5, the same form should be display five times How can i do this with jquery. 例如:如果他输入数字5,则同一表格应显示五次,如何使用jquery做到这一点。

I have attached up to what i have done so far !! 我坚持到现在为止我所做的一切! Thanks in Advance 提前致谢

This is the user input 这是用户输入

<input type="number" class="form-control" id="numberOfChq" >
<a id="come" class="btn btn-primary">Submit</a>

This is the div to be repeated <div id="chk_list" hidden></div> 这是要重复的div <div id="chk_list" hidden></div>

This is the jquery i tried its displaying only once, i want to diplay as many user input 这是我尝试仅显示一次的jQuery,我想显示尽可能多的用户输入

        $("#come").click(function(){
           var chqNumebr = $("#numberOfChq").val();
           var i;
           for (i= 1; i <= chqNumebr; i++ ){
          var chqSh = $("#chk_list").slideDown("slow");
         }
       });

Try the below. 请尝试以下。

First we need something to add the new divs into, so I created <div id="output"></div> for that. 首先,我们需要一些东西来添加新的div,因此我为此创建了<div id="output"></div>

After pressing submit, you'll see the new divs get added according the number the user entered. 按提交后,您将看到根据用户输入的数字添加了新的div。 But we have to make sure that we empty #output every time the function runs otherwise the newly created divs would just keep adding onto the existing ones. 但是我们必须确保每次函数运行时都清空#output否则新创建的div只会继续添加到现有的div上。

Secondly, if we are going to have multiple instances of #chk_list then it shouldn't have an ID as all ID's should be unique. 其次,如果我们将有#chk_list多个实例,则它不应该具有ID,因为所有ID都应该是唯一的。 Instead, we'll use a class. 相反,我们将使用一个类。

 $("#come").on('click', function() { var chqNumebr = $("#numberOfChq").val(); //empty divs that are currently inside of #output $('#output').empty(); for (var i = 0; i < chqNumebr; i++) { $('#output').append('<div class="chk_list"></div>') } }); 
 .chk_list { width: 50px; height: 30px; background: red; margin-top: 10px; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="number" class="form-control" id="numberOfChq"> <a id="come" class="btn btn-primary">Submit</a> <div id="output"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM