简体   繁体   English

append() 函数在网页中不起作用

[英]append() function is not working in webpage

I'm trying to append a form but its not responding.我正在尝试append一个表单,但它没有响应。 Is there any solution for this?有什么解决办法吗?

when i click below button当我点击下面的按钮时

<div class="col-md-4">
  <span href="" class="btn btn-info btn-lg dashboard-icon append">Assign Task</span>
</div>

then it opens然后它打开

paste.ofcode.org/U5pWqiQmsM8BAaUS3dA2X6 paste.ofcode.org/U5pWqiQmsM8BAaUS3dA2X6

by using this:通过使用这个:

  $('.create-task-btn1').click(function(){
     $('.create-task-form1').stop().slideToggle();
  });

then i add "append" class in button and add this code which is below ,,, and after it it stoped working, "its class not id"然后我在按钮中添加“附加”类并添加下面的代码,在它停止工作后,“它的类不是id”

$('.append').click(function() {
  $('table-bordered').append('
      <div class="container">
    <div class="row">
        <form action="#" id="" method="post" class="form-horizontal" role="form" novalidate="novalidate">
            <table class="table table-bordered">
                <tbody>
                    <tr>
                        <th scope="row" style="font-size: 14px;">Task<span class="text-danger">*</span></th>
                        <td colspan="3"><input type="text" class="form-control" value="" name="subject"></td>
                        <td rowspan="3">
                            <a data-id="" data-toggle="modal" data-original-title="" class="enable-tooltip btn" 
                                style="color: red; font-size: 20px;">
                                <i class="fa fa-trash-o"></i>
                            </a>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row" style="font-size: 14px;">Assigned To<span class="text-danger">*</span></th>
                        <td><input type="text" class="form-control" value="" name="subject"></td>
                        <th style="font-size: 14px;">Due Date<span class="text-danger">*</span></th>
                        <td><input type="text" class="form-control" value="" name="subject"></td>
                    </tr>
                </tbody>
            </table>
        </form>
    </div>
    </div>');
});
<div class="col-md-4">
  <span href="" class="btn btn-info btn-lg dashboard-icon append">Assign Task</span>
</div>

simply i wants that when i click on assign btn then below form comes once with finish btn and when i click again and again then it comes without finish button like https://ibb.co/QYCR45P只是我希望当我点击分配 btn 然后下面的表单出现一次完成 btn 并且当我一次又一次点击然后它没有完成按钮像https://ibb.co/QYCR45P

try using this:尝试使用这个:

<div class="col-md-4">
  <span href="" class="btn btn-info btn-lg dashboard-icon append">Assign Task</span>
</div>
 <form class="table-bordered">

 </form>

<script type="text/javascript">
  $('.append').click(function() {
  $('.table-bordered').append('<div class="container"><div class="row"><form action="#" id="" method="post" class="form-horizontal" role="form" novalidate="novalidate"><table class="table table-bordered"><tbody><tr><th scope="row" style="font-size: 14px;">Task<span class="text-danger">*</span></th><td colspan="3"><input type="text" class="form-control" value="" name="subject"></td><td rowspan="3"><a data-id="" data-toggle="modal" data-original-title="" class="enable-tooltip btn" style="color: red; font-size: 20px;"><i class="fa fa-trash-o"></i></a></td></tr><tr><th scope="row" style="font-size: 14px;">Assigned To<span class="text-danger">*</span></th><td><input type="text" class="form-control" value="" name="subject"></td><th style="font-size: 14px;">Due Date<span class="text-danger">*</span></th><td><input type="text" class="form-control" value="" name="subject"></td></tr></tbody></table></form></div></div>');
});

    </script>

You have to provide the template as a single string, ie without line breaks and use tilde operator (`) to specify the start and end of template string.您必须将模板作为单个字符串提供,即没有换行符,并使用tilde operator (`) 指定模板字符串的开始和结束。

I believe you are appending this table to a DOM element (div or form) whose class name is table-bordered :)我相信您将此表附加到类名称为table-bordered的 DOM 元素(div 或表单):)

$('.append').click(function() {
$('.table-bordered').append(`<div class="container"><div class="row"><form action="#" 
id="" method="post" class="form-horizontal" role="form" novalidate="novalidate"><table 
class="table table-bordered"><tbody><tr><th scope="row" style="font-size: 
14px;">Task<span class="text-danger">*</span></th><td colspan="3"><input type="text" 
class="form-control" value="" name="subject"></td><td rowspan="3"><a data-id="" data- 
toggle="modal" data-original-title="" class="enable-tooltip btn" style="color: red; 
font-size: 20px;"><i class="fa fa-trash-o"></i></a></td></tr><tr><th scope="row" 
style="font-size: 14px;">Assigned To<span class="text-danger">*</span></th><td><input 
type="text" class="form-control" value="" name="subject"></td><th style="font-size: 
14px;">Due Date<span class="text-danger">*</span></th><td><input type="text" 
class="form-control" value="" name="subject"></td></tr></tbody></table></form></div> 
</div>`);
});

check it..核实..

 $(document).ready(function(){ $('.append').click(function() { $('#table-bordered').append(`<div class="container"><div class="row"><form action="#" id="" method="post" class="form-horizontal" role="form" novalidate="novalidate"><table class="table table-bordered"><tbody><tr><th scope="row" style="font-size: 14px;">Task<span class="text-danger">*</span></th><td colspan="3"><input type="text" class="form-control" value="" name="subject"></td><td rowspan="3"><a data-id="" data- toggle="modal" data-original-title="" class="enable-tooltip btn" style="color: red; font-size: 20px;"><i class="fa fa-trash-o"></i></a></td></tr><tr><th scope="row" style="font-size: 14px;">Assigned To<span class="text-danger">*</span></th><td><input type="text" class="form-control" value="" name="subject"></td><th style="font-size: 14px;">Due Date<span class="text-danger">*</span></th><td><input type="text" class="form-control" value="" name="subject"></td></tr></tbody></table></form></div> </div>`); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="col-md-4"> <span class="btn btn-info btn-lg dashboard-icon append">Assign Task</span> </div> <div id="table-bordered"> </div>

I think the below code will solve your problem我认为下面的代码将解决您的问题

 $('.append').click(function() { $('.table-bordered').append(`<div class="container"> <div class="row"> <form action="#" id="" method="post" class="form-horizontal" role="form" novalidate="novalidate"> <table class="table table-bordered"> <tbody> <tr> <th scope="row" style="font-size: 14px;">Task<span class="text-danger">*</span></th> <td colspan="3"><input type="text" class="form-control" value="" name="subject"></td> <td rowspan="3"> <a data-id="" data-toggle="modal" data-original-title="" class="enable-tooltip btn" style="color: red; font-size: 20px;"> <i class="fa fa-trash-o"></i> </a> </td> </tr> <tr> <th scope="row" style="font-size: 14px;">Assigned To<span class="text-danger">*</span></th> <td><input type="text" class="form-control" value="" name="subject"></td> <th style="font-size: 14px;">Due Date<span class="text-danger">*</span></th> <td><input type="text" class="form-control" value="" name="subject"></td> </tr> </tbody> </table> </form> </div> </div>`); });
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script> <div class="col-md-4"> <span class="btn btn-info append">Assign Task</span> </div> <div class="table-bordered"></div>

If you want to add multiple line of html element so there is need to add this ` operator.如果要添加多行 html 元素,则需要添加此`运算符。
I hope below snippet will help you lot.我希望下面的片段会对你有很大帮助。

 $('.append').click(function() { $('.append-target').append(`<div class="container"> <div class="row"> <div class="col-sm-12"> <form action="#" id="" method="post" class="form-horizontal" role="form" novalidate="novalidate"> <table class="table table-bordered"> <tbody> <tr> <th scope="row" style="font-size: 14px;">Task<span class="text-danger">*</span></th> <td colspan="3"><input type="text" class="form-control" value="" name="subject"></td> <td rowspan="3"> <a data-id="" data-toggle="modal" data-original-title="" class="enable-tooltip btn" style="color: red; font-size: 20px;"> <i class="fa fa-trash-o"></i> </a> </td> </tr> <tr> <th scope="row" style="font-size: 14px;">Assigned To<span class="text-danger">*</span></th> <td><input type="text" class="form-control" value="" name="subject"></td> <th style="font-size: 14px;">Due Date<span class="text-danger">*</span></th> <td><input type="text" class="form-control" value="" name="subject"></td> </tr> </tbody> </table> </form> </div> </div> </div>`); });
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container my-3"> <div class="row"> <div class="col-md-4"> <span href="" class="btn btn-info btn-lg dashboard-icon append">Assign Task</span> </div> </div> </div> <div class="append-target"></div>

I suppose you'd put your code in a empty tag.我想你会把你的代码放在一个空标签中。 Your code is not clear but I can help you.您的代码不清楚,但我可以帮助您。 Take a look of this piece of code, maybe is useful:看看这段代码,也许有用:

--- Before at all you need a span to append your form: --- 在你需要一个跨度来附加你的表单之前:

<div class="col-md-12">
    <span class="table-append"></span>
</div>

--- In your script tag (put in at the end before the ): --- 在您的脚本标记中(放在 之前的末尾):

<script>
        $('.append').click(function() {
            $(".table-append").append(`<div class="container">
                <div class="row">
                    <form action="#" id="" method="post" class="form-horizontal" role="form" novalidate="novalidate">
                        <table class="table table-bordered">
                            <tbody>
                                <tr>
                                    <th scope="row" style="font-size: 14px;">Task<span class="text-danger">*</span></th>
                                    <td colspan="3"><input type="text" class="form-control" value="" name="subject"></td>
                                    <td rowspan="3">
                                        <a data-id="" data-toggle="modal" data-original-title="" class="enable-tooltip btn" 
                                            style="color: red; font-size: 20px;">
                                            <i class="fa fa-trash-o"></i>
                                        </a>
                                    </td>
                                </tr>
                                <tr>
                                    <th scope="row" style="font-size: 14px;">Assigned To<span class="text-danger">*</span></th>
                                    <td><input type="text" class="form-control" value="" name="subject"></td>
                                    <th style="font-size: 14px;">Due Date<span class="text-danger">*</span></th>
                                    <td><input type="text" class="form-control" value="" name="subject"></td>
                                </tr>
                            </tbody>
                        </table>
                    </form>
                </div>
            </div>`);
        });
    </script>

Your table code is put between ´´ to avoid concatenated strings.您的表代码放在 ´´ 之间以避免串联字符串。

Hope this answer can help you!希望这个回答能帮到你!

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

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