I have a code that works in .click() but that only works for the first row. I need to use the same ID as the data is dynamically generated.
code perfectly works for first row .. and grabs the TR ID but doesnt work for 2nd row or so.<tr id="25">
<td>25</td>
<td>asd</td>
<td>gjkhgkj</td>
<td></td>
<!-- more td's -->
<td id="edit" name="25">开发者_JS百科Edit</td>
<td id="delete" name="25">Delete</td>
</tr>
<tr id="24">
<td>24</td>
<td>hhh</td>
<td></td>
<!-- more td's -->
<td id="edit" name="24">Edit</td>
<td id="delete" name="24">Delete</td>
</tr>
The function to delete the customer data:
$('#delete').click(function(e) {
var bid = this.id; // button ID
var customerid = $(this).closest('tr').attr('id'); // table row ID
$.ajax({
type: "POST",
url: "delete.php",
data: "id="+customerid+"",
success: function(msg){
// $('#'+customerid).css({backgroundColor: 'red'});
$('#'+customerid).remove();
alert("Sucessfully Deleted.")
// $('#tabledata').load('load.php');
}
});//end of $ajax
I need to use the same ID as the data is dynamically generated.
You cannot use the same ID for multiple elements on a single page. If you need to dynamically generate the elements then use a class instead.
<td class="delete" name="24">Delete</td>
$('.delete').click(function(e) {
if your has an ID which is the database ID which must be taken as an action in your AJAX stream, then try the following:
$('#delete, #edit').click(function(e) {
var myID = $(this).attr('name');
var myAction = $(this).html();
var myRow = $(this).parent('tr');
alert('You have choosen to ' + myAction + ' the row (' + myID + ').');
});
Of course, after that, you can do whatever you want ;)
Since an ID must be unique, the event is only binded to the first element with the id "delete". What you want is an class on that element and than try again. Another thing, maybe parent('tr') makes more sense than closest().
If you cannot add a class instead of an id (which is a bit weird in the first place), you can also use the 'contains' selecter to look for the string 'delete'. Like so:
$("td:contains('delete')")
This is because you are using an id selector, wich returns only one element and not a class selector. You should change your html and use classes
You cannot use the same ID!
ID has to be unique.
Can't you just make it a class.
Or perhaps if you need that number make it into a data attribute.
精彩评论