Sto eseguendo uno script jQuery davvero semplice per afferrare tutte le email selezionate dalle checkbox in una tabella. La tabella si presenta così:
First Name Email Address
Il mio jQuery ha questo aspetto:
$("#submitButton").click(function() { var output = []; $("table tbody tr:has(input:checkbox:checked)").each(function() { var email = $('td.email', $(this)).text(); if (validate(email) && output.indexOf(email) == -1) output.push(email); }); $("#emails").val(output.join(", ")); }); function validate(email) { return /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/.test(email); }
Questo fallisce miseramente in IE, ma funziona ovunque.
table tbody tr:has(input:checkbox:checked)
selettore table tbody tr:has(input:checkbox:checked)
non corrisponde a nulla. Object expected
. PERCHÉ!? JQuery non è progettato per essere cross-browser e portatile?
Internet Explorer (<9) non ha Array.prototype.indexOf
. Prova ad usare invece $.inArray
jQuery (è cross browser e in effetti utilizzerà Array.prototype.indexOf
se esiste :-P).
if (validate(email) && $.inArray(email,output) == -1) output.push(email);
dai un input alla class e indirizzalo direttamente …
First Name Email Address
Quindi scegli come target quelli controllati nel tuo js in questo modo:
$("#submitButton").click(function() { var output = []; $(".emailchecked ([checked='checked'])").each(function() { var email = $('td.email', $(this)).text(); if (validate(email) && output.indexOf(email) == -1) output.push(email); }); $("#emails").val(output.join(", ")); }); function validate(email) { return /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/.test(email); }
[email protected] [email protected] [email protected] [email protected]
Since you have three columns instead of two, just walk the DOM for that row.