2017-04-20 241 views
0

我一直在创建一个应用程序,它基于您在一个文本框中输入的数字添加表格。我试图弄清楚每次“Go”按钮被第二次点击后,我生成的以前的表格消失了。到目前为止,我可以添加表格,但他们只是保持克隆。jquery第二次点击删除克隆?

我试着用'看过'的对象删除以前的表,但无法弄清楚如何真正让它处理我的代码。我抬头看.remove,但不知道在我的代码中究竟在哪里工作。

真人版:http://codepen.io/BabinecJ/pen/BRjJbw

$('[name="cand_no" ]').on('change', function() { 
 
    // Not checking for Invalid input 
 
    if (this.value != '') { 
 
    var val = parseInt(this.value, 10); 
 
    ///Click add button add count number + table 
 

 
    $(function() { 
 
     $('#add').bind('click', function() { 
 
     $('#mytbody'); 
 
     var count = $('#mytbody').children('tr').length; 
 
     $('#counter').html(count); 
 

 
     ///Adding coder name 
 
     var name = $("#tname").val(); 
 
     $('#aname').html(name); 
 
     }); 
 
    }); 
 
    
 
    /// Figuring out way to disable enter key being pressed 
 
    $(document).ready(function() { 
 
     $('cand_no').keydown(function(event) { 
 
     if (event.keyCode == 13) { 
 
      event.preventDefault(); 
 
      return false; 
 
     } 
 
     }); 
 
    }); 
 

 
    for (var i = 0; i < val; i++) { 
 
     // Clone the Template 
 
     var $cloned = $('.template tbody').clone(); 
 

 
     // For each number added append the template row 
 
     $('#studentTable tbody').append($cloned.html()); 
 
    } 
 
    } 
 

 
    var seen = {}; 
 
    $('a').each(function() { 
 
    var txt = $(this).text(); 
 
    if (seen[txt]) 
 
     $(this).remove(); 
 
    else 
 
     seen[txt] = true; 
 
    }); 
 
});
.template { 
 
    border-style: solid; 
 
} 
 

 
#cand_no { 
 
    background-color: red; 
 
} 
 

 
#add { 
 
    color: blue; 
 
    position: absolute; 
 
    margin-left: -900px; 
 
    margin-top: -35px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p> 
 
    <label><strong>Number of Rows</strong></label> 
 
    <label><input name="cand_no" type="text" placeholder="Type Number of Rows" id="cand_no" /></label> 
 

 
    <div style="float: right; width: 40px"> 
 
    <button id="add">GO!</button> 
 
    </div> 
 
    <div class="clear"></div> 
 
</p> 
 
<p> 
 
    <label><strong>Your Name</strong></label> 
 
    <label><input name="tname" id="tname" type="text" placeholder="Type Your Name" /></label> 
 
    <div class="clear"></div> 
 
</p> 
 

 
<div class="cand_fields"> 
 
    <table id="studentTable" "txt" width="630" border="solid"> 
 
    <tbody id="mytbody"> 
 
     <tr> 
 
     <td> 
 
      <p>Number of rows: 
 
      <p id="counter"> 
 
       </span> 
 
      </p> 
 
     </td> 
 

 
     <td id="tname" width="0">Name 
 
      <p id="aname"> 
 
      </span> 
 
      </p> 
 
     </td> 
 
     </tr> 
 
     <tr> 
 
     <td><input name="tname" type="text" placeholder="" required="required" id="tname" /></td> 
 

 
     <td><input name="cand_pos" type="text" placeholder="" required="required" /></td> 
 
     </tr> 
 
    </table> 
 
</div> 
 

 
<div class="template" id=".template" style="display:none "> 
 
    <table> 
 

 

 

 

 

 
    <tr> 
 
     <td><input name="cand_name" type="text" placeholder="" required="required" id="count" /></td> 
 

 
     <td><input name="cand_pos" type="text" placeholder="" required="required" /></td> 
 
    </tr> 
 
    </tbody> 
 
    </table> 
 
</div>

+0

你是指表格行还是表格? – ConorReidd

+0

@ConorReidd表行 – snakeeyes08

回答

0

好了,正把它多一点,而不是找你的代码的解决方案后,我试着去适应你的代码,以便它匹配你想要做得更好的东西。

我创建了一个http://jsfiddle.net/j1sqej3w/为你做(我相信)你想要做的。

jQuery的我用的是:

$(document).ready(function() { 
    $("form").submit(function() { 
    var times = $("input").val(); 
    $("tr").slice(2).remove(); 
    for(var i=0; i < times; i++){ 
     $("table").append("<tr><td>row 1</td><td>row 2</td></tr>"); 
    } 
    return false; 
    }); 
}); 

通知其保持了前两个表中的行完整和原来为了与他人消失,下一次添加的值$("tr").slice(2).remove();

做任务,如删除输入要添加表行可以通过将<form><div>http://jsfiddle.net/66x3n11f/

我希望我在正确的轨道上完成。如果我误解了,请告诉我。

+0

是的!非常感谢! – snakeeyes08

+0

如果您可以将此标记为正确的答案,可以帮助其他遇到类似问题的人找到解决方案。 :) – ConorReidd