I have a page with a number of tables generated by php, mysql. And some jquery I would like to run on each table.
Im thinking the best way to do it would be to create an array of the table ids and run the jquery in a foreach.
Thing is I have no idea how to do this so if anyone has any idea on how to do this it would be much appreciated.
EDIT/// This is the code I would like to run on each table
$("#frontshade:radio").click(function() {
$(this).parent().children(":checkbox").attr('checked', false);
$(this)
.closest('table') // find the parent row
.find(":input[type='text']") // find text elements in that row
.attr('disabled','disabled') // disable them
.removeClass('disabled'); //Toggle class
$("#frontshade :checkbox").hide();
$("#frontshade .cbox").hide();
$(this开发者_开发知识库).parent().children(":checkbox").show();
$(this).parent().children(".cbox").show();
});
$("#frontshade :checkbox").click(function(){
$(this)
.closest('tr') // find the parent row
.find(":input[type='text']") // find text elements in that row
.toggleDisabled()
.toggleClass('disabled','slow') // enable them
.end() // go back to the row
.siblings() // get its siblings
.find(":input[type='text']") // find text elements in those rows
.attr('disabled','disabled')
.removeClass('disabled'); // disable them
});
To iterate through the tables and grab their ids, as well as perform any other operation you would want:
var table_ids = new Array();
$('table')
.each(function(e){
table_ids[] = $(this).attr('id');
//do other stuff with each table
});
$('table').each(function() {
// do stuff with table
});
Or do you mean MySQL tables?
You know, the following code would achieve the same:
jQuery('table').each(function() {
// your code here
});
More details in the docs.
Something like this should get you started,
$('table').each(function(index, table){
table.find('td').each(function(index, el){
el.doSomething();
});
})
If you want the tables to have the same script, you could also add a class to the table and say:
$('table.doIt').each(function ()
{
// your code here
}
I think getting all ID's is a bit awkward, because just adding a class to the tables you want to run the jQuery on seems a bit easier. It costs less effort in JS and just one extra class addition in you PHP.
精彩评论