Rewriting A Part of My Code
I'm trying to come up with a better way of accomplishing a task. I have about 30 different data tables. One for each module and they ALL share a button with a class of edit. The edit button takes the id of the button which is the DB id row and takes it to the module's edit form page. So keeping all of this in mind to prevent bubbling and all other else. Does anyone know a better way to write this.
This is just an example of how one is written.
$('.edit').live('click', function(e) {
e.preventDefault();
var templateID = $(this).attr('id');
if(!$('div.right_content').hasClass("loading")){
$('div.right_content').addClass("loading").load('modules/forms/edit/templates.php?templateID=' + templateID,
function() {
$(this开发者_运维百科).removeClass("loading");
});
}
});
EDIT:
<table id="templatesPageList" class="rounded-corner dataTablePageList">
<thead>
<tr>
<th scope="col" class=""></th>
<th scope="col" class="">ID</th>
<th scope="col" class="">Description</th>
<th scope="col" class="">Edit</th>
<th scope="col" class="">Delete</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="templates" value="1" /></td>
<td>1</td>
<td>Main Site Template</td>
<td><a href="#"><img src=
"http://www.kansasoutlawwrestling.com/manager/images/user_edit.png" class="edit"
alt="" title="" border="0" id="1" name="1" /></a></td>
<td><a href="#" class="ask"><img src=
"http://www.kansasoutlawwrestling.com/manager/images/trash.png" class="delete"
alt="" title="" border="0" id="1" name="1" /></a></td>
</tr>
<tr>
<td><input type="checkbox" name="templates" value="2" /></td>
<td>2</td>
<td>Event Page</td>
<td><a href="#"><img src=
"http://www.kansasoutlawwrestling.com/manager/images/user_edit.png" class="edit"
alt="" title="" border="0" id="2" name="2" /></a></td>
<td><a href="#" class="ask"><img src=
"http://www.kansasoutlawwrestling.com/manager/images/trash.png" class="delete"
alt="" title="" border="0" id="2" name="2" /></a></td>
</tr>
</tbody>
</table>
Your code looks fine; if you want to improve performance, you can use .delegate() instead of .live(). In general, you should always use .delegate() over .live().
$('tr').delegate('.edit', function(e) {...});
Refer to this thread for specifics on the performance improvements: Jquery live() vs delegate()
精彩评论