[英]Add and remove buttons wont work fine Jquery
Hi I am having this issue where my add buttons work only if they are outside the function and my remove buttons only works when they are inside the function.嗨,我遇到了这个问题,我的添加按钮仅在函数外部时才起作用,而删除按钮仅在函数内部时起作用。 I've tried putting the add buttons outside and only remove buttons inside the group and they work fine.
我试过将添加按钮放在外面,只删除组内的按钮,它们工作正常。 I am using jquery.
我正在使用 jquery。 Can someone please help and explain me how to fix this?
有人可以帮我解释一下如何解决这个问题吗? Thank you!
谢谢!
// add row $("#addRow").click(function() { var html = ''; html += '<div class="container">'; html += ' <div id="card-group">'; html += '<div class="card card-outline card-warning">'; html += '<div class="card-header">Panel with panel-primary class</div>'; html += '<div class="card-body">'; html += '<label>Main Question</label>'; html += '<input type="text" class="form-control">'; html += ' <div id="card-subody" style="padding: 50px;">'; html += '<label>Sub Question</label>'; html += '<div id="newsubRow"></div>'; html += '<div id="card-subtext">'; html += '<input type="text" class="form-control">'; html += ' <button id="addgrpRow" type="button" class="btn btn-success">This add button doesnt work</button>'; html += ' <button id="removegrpRow" type="button" class="btn btn-danger">This Remove button works</button>'; html += '</div>'; html += '</div>'; html += '</div>'; html += '</div>'; html += '<button id="addRow" type="button" class="btn btn-info">This add group button doesnt work</button>'; html += '<button id="removeRow" type="button" class="btn btn-danger">This Remove Group button works</button>'; html += '</div>'; html += '</div>'; $('#newRow').append(html); }); $("#addgrpRow").click(function() { var html = ''; html += '<div id="card-subtext">'; html += '<input type="text" class="form-control" placeholder="Example: SO1.">'; html += '<button id="addgrpRow" type="button" class="btn btn-success">This add sub question button doesnt work</button>'; html += '<button id="removegrpRow" type="button" class="btn btn-danger">This Remove sub question button works</button>'; html += '</div>'; $('#newsubRow').append(html); }); $(document).on('click', '#removeRow', function() { $(this).closest('#card-group').remove(); }); // remove row $(document).on('click', '#removegrpRow', function() { $(this).closest('#card-subtext').remove(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> <body> <div id="newRow"></div> <div class="container"> <div id="card-group"> <div class="card card-outline card-warning"> <div class="card-header"> <h3 class="card-title">Group</h3> </div> <div class="card-body"> <label>Main Question</label> <input type="text" class="form-control"> <div id="card-subody" style="padding: 50px;"> <label>Sub Question</label> <div id="newsubRow"></div> <div id="card-subtext"> <input type="text" class="form-control"> <button id="addsubRow" type="button" class="btn btn-success">This add sub question button doesnt work</button> <button id="removegrpRow" type="button" class="btn btn-danger">This Remove sub question button works</button> </div> </div> </div> </div> </div> <button id="addgrpRow" type="button" class="btn btn-success">This add sub question button works</button> <button id="addRow" type="button" class="btn btn-info">This add group button works</button> <button id="removeRow" type="button" class="btn btn-danger">This Remove Group button doesnt work</button> </div> </body>
There doesn't seem to be a function for 'addsubRow' so that's why that button won't work.似乎没有“addsubRow”的功能,这就是该按钮不起作用的原因。
Using closest() method won't work like you intend to.使用closest() 方法不会像您打算的那样工作。 You are trying to remove an element which is in an entirely different div.
您正在尝试删除一个完全不同的 div 中的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.