基于他人代码开发网页时,对jQuery动态添加表格行代码存疑
Hey Diego, let's break down this code line by line to unpack exactly what it's doing—dynamic table row logic can feel opaque at first, but once we walk through each part, it'll click!
Breakdown of the Dynamic Table Row Code
First, let's restate the code clearly so we can dissect it:
$('#botton').on('click',function(){ var table = $('#table') ; var tr = table.find('tr').first().clone().appendTo(table); tr.find('select').selectpicker('val', ''); tr.find('input').val(''); });
What each section does:
$('#botton').on('click',function(){ ... }): This attaches a click event listener to an element with the IDbotton(quick note: that's a typo for "button"—if your actual button uses the correct spellingbutton, this code won't trigger at all! That's a common gotcha). When this button is clicked, the function inside runs.var table = $('#table') ;: Grabs the table element with IDtableand stores it in a variable. This is a efficiency trick—querying the DOM once and reusing the reference is faster than searching for the table every time we need it.var tr = table.find('tr').first().clone().appendTo(table);: This is the core row-adding logic:table.find('tr').first(): Fetches the very first row in the table (this is supposed to be your "template" row that you want to duplicate)..clone(): Creates a full deep copy of that template row—all its<td>cells, inputs, dropdowns, and even styling get duplicated..appendTo(table): Adds this cloned row to the bottom of the table. Since we chain this method, thetrvariable ends up holding the brand-new cloned row element for us to modify.
tr.find('select').selectpicker('val', '');: If you're using the Bootstrap Select plugin (marked by theselectpickermethod), this resets any dropdowns in the new row to an empty value. Without this line, the cloned row would keep the same selection as the original template.tr.find('input').val('');: Clears out all text inputs in the new row, so they don't inherit the values from the template row.
Common confusion points (and fixes):
- Template row mix-up: If your first table row is a header (in
<thead>), cloning it will add another header instead of a data row. Fix this by targeting data rows specifically withtable.find('tbody tr').first(). - Missing event listeners: If your original inputs/dropdowns have event handlers (like change or blur events), the cloned rows won't automatically inherit those. Use event delegation (e.g.,
$(document).on('change', '#table input', function(){ ... })) to handle events on dynamically added elements. - Bootstrap Select rendering issues: Sometimes the cloned dropdown won't display correctly. Add
tr.find('select').selectpicker('refresh')right after setting the value to make sure the plugin recognizes the new element. - The
bottontypo: As mentioned earlier, if your button's ID isbutton(the correct spelling), this code will never run. Double-check your HTML element's ID!
Hopefully this clears up exactly what's happening in that snippet. If you're stuck on a specific part of the logic, feel free to ask more details!
内容的提问来源于stack exchange,提问作者Diego
相关产品推荐
相关产品推荐

