在onclick上找到最接近的div并追加div

时间:2019-04-27 07:07:02

标签: javascript php jquery append closest

这里我想使用最接近的函数将div附加到父div。这是我的代码,请看看

    var maxAppends = 0;
    function add_field(id)
     {
      
    var update_new = $('<div class="form-group" style="margin-bottom: 0px">\n\
                      <label for="field-1" class="col-sm-4 control-label">Documentsss</label>\n\
                      <div class="col-sm-5">\n\
                      <div class="fileinput fileinput-new" data-provides="fileinput">\n\
                      <span class="btn btn-default btn-file"><span class="fileinput-new" >Select file</span><span class="fileinput-exists" >Change</span><input type="file" name="files[]" ></span> <span class="fileinput-filename"></span><a href="#" class="close fileinput-exists" data-dismiss="fileinput" style="float: none;">&times;</a></div></div>\n\<div class="col-sm-2">\n\<strong>\n\
                      <a href="javascript:void(0);" class="remCF"><i class="fa fa-times"></i>&nbsp;Remove</a></strong></div>');

    $(".update_new_"+id).append(update_new);
    maxAppends++;
         
       
    }
    <div id="update_new_<?php echo $value->id;?>" class="update_new_<?php echo $value->id;?> item-row">
                <div class="form-group mb0" style="margin-bottom: 0px">
                    <label for="field-1"
                           class="col-sm-4 control-label">Other Document</label>
                    <div class="col-sm-5">
                        <div class="fileinput fileinput-new" data-provides="fileinput">
                            <?php
                            if (!empty($value->documents)) {
                                $uploaded_file = json_decode($value->documents);
                            }
                            if (!empty($uploaded_file)):foreach ($uploaded_file as $v_files_image): ?>
                                <div class="">
                                    <input type="hidden" name="fileName[]"
                                           value="<?php echo $v_files_image ?>">
                                  
                                    <span class=" btn btn-default btn-file">
                                    <span class="fileinput-filename"> <?php echo $v_files_image ?></span>
                                    <a href="javascript:void(0);" class="remCFile" style="float: none;">×</a>
                                    </span>
                                    <strong>
                                        <a href="javascript:void(0);" class="RCF"><i
                                                class="fa fa-times"></i>&nbsp;Remove</a></strong>
                                    <p></p>
                                </div>

                            <?php endforeach; ?>
                            <?php else: ?>
                                <span class="btn btn-default btn-file"><span
                                        class="fileinput-new">Select File</span>
                                                            <span class="fileinput-exists"><?= lang('change') ?></span>
                                                            <input type="file" name="files[]">
                                                        </span>
                                <span class="fileinput-filename"></span>
                                <a href="#" class="close fileinput-exists" data-dismiss="fileinput"
                                   style="float: none;">&times;</a>
                            <?php endif; ?>
                        </div>
                        <div id="msg_pdf" style="color: #e11221"></div>
                    </div>
                    <div class="col-sm-3">
                        <strong><a href="javascript:void(0);" id="update_more" onclick="add_field('<?php echo $value->id;?>');" u_id="<?php echo $value->id;?>" class="addCF item-row-click update_more_<?php echo $value->id;?>"><i
                                    class="fa fa-plus"></i>&nbsp;Add More
                            </a></strong>
                    </div>
                </div>

                

            </div>

当我单击add_field时,我想将div附加到具有类div的父项update_new_<?php echo $value->id;?>上,因此为了执行该脚本,请执行以下操作:外观

在不使用最接近函数的情况下,div正在追加,但是div的值未通过post传递值,所以我想使用最接近函数来执行append函数,请帮助我解决

0 个答案:

没有答案