javascript - 显示数据后不显示添加按钮

标签 javascript php jquery ajax codeigniter

ajax 成功后,数据显示在行中,但如果我当时想添加新行,则添加按钮不显示。 enter image description here

enter image description here

我的代码:

  <div class="row">
     <div class="col-12">
      <h3>Add Video Detail</h3>
       <form id="insertvideo" method="post">
       <table id="addrow" width="100%">
        <td><input type="button" class="btn btn-success addButton col-3 offset-1" value="add"/></td>
          <tr class="clonetr">
                <td>Video Title<input type="text" id="videotitle" name="videotitle[]" class="form-control"></td>
                <td>Video description<input type="text"  id="videodesc" name="videodesc[]" class="form-control"></td>
                <td>Video Links<input type="text"  id="videolink" name="videolink[]" class="form-control"></td>
                <td><input type="button" class="btn btn-danger deleteButton" value="delete"/></td>
          </tr>
       </table>
       </form>  
     </div>
   </div>
   <div class="col-sm-6" style="margin-top: 13px; margin-left: 400px;">
       <button type="submit" id="btn-update"   value="Submit" class="btn btn-primary col-3 offset-1">Save</button>
   </div>

  <script>
      $(function(){
        $(".addButton").click(function(){
            $('.clonetr:last').clone(true).appendTo("#addrow");
        });

        $(".deleteButton").click(function(){
            if($('.deleteButton').length > 1)

               $(this).closest("tr").remove();
            else
                alert('Atleast one required.');

        });
    });
   var mainCatId = $(this).val();
       $.ajax({
                url: "<?php echo base_url();?>admin_controller/WebsiteContentController/fetchSubDetail",
                type: "POST",
                data: {mainCatId:mainCatId},
                dataType:'html',
                success: function(response)
                {
                    $('#addrow').html(response);
                    console.log(response);
                }
             });

   </script>

我不知道我的代码哪里错了。 我想添加一个新行,但添加按钮显示以添加一个新行。

最佳答案

因为我不能运行你的 ajax,这只是一个关于如何修复它的猜测。

首先,通过添加缺失的 <tr> 来修复表:

<table id="addrow" width="100%">
  <tr>
    <td><input type="button" class="btn btn-success addButton col-3 offset-1" value="add" /></td>
  </tr>
  <tr class="clonetr">
    <td>Video Title<input type="text" id="videotitle" name="videotitle[]" class="form-control"></td>
    <td>Video description<input type="text" id="videodesc" name="videodesc[]" class="form-control"></td>
    <td>Video Links<input type="text" id="videolink" name="videolink[]" class="form-control"></td>
    <td><input type="button" class="btn btn-danger deleteButton" value="delete" /></td>
  </tr>
</table>

现在清除表格但保留添加按钮:

$('#addrow tr:not(:first)').remove();
$('#addrow').append(response);

所以现在它看起来像:

$.ajax({
    url: "<?php echo base_url();?>admin_controller/WebsiteContentController/fetchSubDetail",
    type: "POST",
    data: {mainCatId:mainCatId},
    dataType:'html',
    success: function(response)
    {
        $('#addrow tr:not(:first)').remove();
        $('#addrow').append(response);
        console.log(response);
    }
});

要清除新创建的行,尝试:

$(".addButton").click(function(){
    $('.clonetr:last').clone(true).appendTo("#addrow");
    $("#addrow tr:last input").val("");
});

关于javascript - 显示数据后不显示添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57918604/

相关文章:

javascript - 如何使用 Jquery 和 Json API 通过谷歌地图 API 使用坐标显示城市名称?

javascript - 使用嵌入式 PDF 检测 iframe 中的鼠标和键盘事件

php - mysql计算两个日期时间之间的结果

php - 单击后 Selenium php-webdriver 检查属性

javascript - 使用 JQuery 激活 Bootstrap 选项卡

jquery - 如何使用 jQuery 和 $form 助手添加新的表单字段?

javascript - 在 arcgis JavaScript 中添加草皮后无法显示图形图层

javascript - 为什么 Google map RichMarker 库中的 zIndex 不起作用?

javascript - 为什么对象的方法中用的是对象名而不是this呢?

php - 按需并排排列 DIV - HTML PHP