简体   繁体   English

如何在jQuery函数的append中使用循环

[英]How to use loop inside append in jQuery function

i would like to append a new html code, buy clicking a button. 我想附加一个新的html代码,请购买单击一个按钮。 however in the append there are a looping to show the html code. 但是在附录中有一个循环来显示html代码。

<script>
var day_left = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
$(".add_schedule").click(function () {
                if ($('.day-part').is(':empty')){
                    alert("You have limit schedule time!")
                }else{
                    var stack = $(this).parent().find('#stack').val();
                    alert("form-schedule-"+stack);
                    $(this).before('<div class="form-schedule" id="form-schedule-'+stack+'">' +
                        '<div class="col-md-4">' +
                        '<label>Start Time</label><input type="time" placeholder="ex : 00:00" name="time['+stack+'][open_time]" class="open_time form-control" id="open_time'+stack+'">' +
                        '</div>' +
                        '<div class="col-md-4">' +
                        '<label>End Time</label><input type="time" placeholder="ex : 21:00" name="time['+stack+'][close_time]" class="close_time form-control" id="close_time"></div>' +
                        '<div class="col-md-4">' +
                        '<label>Pilih Hari</label> ' +
                        '<div class="dropdown dropdown-payment">' +
                        '<button class="btn btn-primary btn-sm dropdown-toggle" type="button" data-toggle="dropdown">Select Date <span class="caret"></span></button> ' +
                        '<div class="dropdown-menu"><div class="everyday-part">' +
                        '<li> <label> ' +
                        '<input type="checkbox" id="everyday" class="everyday_check_box form form-control" name="time['+stack+'][everyday]" value="7"> Everyday </label>' +
                        '</li>' +
                        '</div>' +
                        '<div class="day-part">'+

       // The Problem is Started From Here
                        $.each(day_left,function (i,val) {
                            $('<li><label><input type="checkbox" checked="checked" id="date_check_box" class="form form-control" name="time[' + stack + '][date][]" value="' + i + '">'+val+'</label> </li>');
                        })+'</div> </div> </div> </div></div><div class="clearfix"></div><br> ');


       // End Of the Problem

                    stack = parseInt(stack)+1;



                    $(this).parent().find('#stack').val(stack);

                }


            });
</script>

actually i could append the html. 实际上我可以附加html。 But there is a bug in my $.each(day_left,function(i,val)){}); 但是我的$.each(day_left,function(i,val)){});有一个错误$.each(day_left,function(i,val)){});

its only return the value of day_left which they are like sunday,monday,... . 它仅返回day_left的值,它们类似于sunday,monday,... The code doesn't show the html code 该代码不显示html代码

My expected is it would be returned like this 我期望它会像这样返回 在此处输入图片说明

but my code is only return : 但是我的代码只是返回: 在此处输入图片说明

Please make a function like this 请做一个这样的功能

 function makeString(day_left,stack){
              var str = '';
              for(var i = 0; i <= day_left.length ; i++ ){
                str += '<li><label><input type="checkbox" checked="checked" id="date_check_box" class="form form-control" name="time[' + stack + '][date][]" value="' + i + '">'+day_left[i]+'</label> </li>';
              }
              return str;
            }

and change as following 并进行如下更改

$(this).before('<div class="form-schedule" id="form-schedule-'+stack+'">' +
                    '<div class="col-md-4">' +
                    '<label>Start Time</label><input type="time" placeholder="ex : 00:00" name="time['+stack+'][open_time]" class="open_time form-control" id="open_time'+stack+'">' +
                    '</div>' +
                    '<div class="col-md-4">' +
                    '<label>End Time</label><input type="time" placeholder="ex : 21:00" name="time['+stack+'][close_time]" class="close_time form-control" id="close_time"></div>' +
                    '<div class="col-md-4">' +
                    '<label>Pilih Hari</label> ' +
                    '<div class="dropdown dropdown-payment">' +
                    '<button class="btn btn-primary btn-sm dropdown-toggle" type="button" data-toggle="dropdown">Select Date <span class="caret"></span></button> ' +
                    '<div class="dropdown-menu"><div class="everyday-part">' +
                    '<li> <label> ' +
                    '<input type="checkbox" id="everyday" class="everyday_check_box form form-control" name="time['+stack+'][everyday]" value="7"> Everyday </label>' +
                    '</li>' +
                    '</div>' +
                    '<div class="day-part">'+

   // The Problem is Started From Here
                   makeString(day_left,stack)+'</div> </div> </div> </div></div><div class="clearfix"></div><br> ');

plnkr https://plnkr.co/edit/aBLj49Yv2g4EakeKjlqI?p=preview plnkr https://plnkr.co/edit/aBLj49Yv2g4EakeKjlqI?p=preview

you can do like that...... just use return function or make you iteration returnable. 您可以那样做……只需使用return函数或使您的迭代可返回。

 <script>
var day_left = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
$(".add_schedule").click(function () {
                if ($('.day-part').is(':empty')){
                    alert("You have limit schedule time!")
                }else{
                    var stack = $(this).parent().find('#stack').val();
                    alert("form-schedule-"+stack);
                    $(this).before('<div class="form-schedule" id="form-schedule-'+stack+'">' +
                        '<div class="col-md-4">' +
                        '<label>Start Time</label><input type="time" placeholder="ex : 00:00" name="time['+stack+'][open_time]" class="open_time form-control" id="open_time'+stack+'">' +
                        '</div>' +
                        '<div class="col-md-4">' +
                        '<label>End Time</label><input type="time" placeholder="ex : 21:00" name="time['+stack+'][close_time]" class="close_time form-control" id="close_time"></div>' +
                        '<div class="col-md-4">' +
                        '<label>Pilih Hari</label> ' +
                        '<div class="dropdown dropdown-payment">' +
                        '<button class="btn btn-primary btn-sm dropdown-toggle" type="button" data-toggle="dropdown">Select Date <span class="caret"></span></button> ' +
                        '<div class="dropdown-menu"><div class="everyday-part">' +
                        '<li> <label> ' +
                        '<input type="checkbox" id="everyday" class="everyday_check_box form form-control" name="time['+stack+'][everyday]" value="7"> Everyday </label>' +
                        '</li>' +
                        '</div>' +
                        '<div class="day-part">'+

       // The Problem is Solved
            function(){var s='';
        $.each(day_left,function (i,val) {
                            s += '<li><label><input type="checkbox" checked="checked" id="date_check_box" class="form form-control" name="time[' + stack + '][date][]" value="' + i + '">'+val+'</label> </li>';
                        });
               return s;}
+'</div> </div> </div> </div></div><div class="clearfix"></div><br> ');


       // End Of the Problem

                    stack = parseInt(stack)+1;



                    $(this).parent().find('#stack').val(stack);

                }


            });
</script>

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

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