jquery: click for every id-element starting with
I tried to find all id
s starting with the string "matchItem_" and define a click-event for each. onclick i want wo execute a script with a URL parameter and change the image in this id-element. Unfortunately my syntax isn't right, I also tried with .each.function
but I did开发者_C百科n't get it.
$('[id^="matchItem_"]').each {
$(this).click(){
//...execute my script.php?urlparam=xx....;
$(this).find('img').attr('src','/admin/images/ok.png');
}
}
You don't need the each
:
$('[id^="matchItem_"]').click(function() {
// do something
$(this).find('img').attr('src','/admin/images/ok.png');
});
Your syntax is invalid because you forgot the functions.
$('[id^="matchItem_"]').each(function() {
$(this).click(function(){
//...execute my script.php?urlparam=xx....;
$(this).find('img').attr('src','/admin/images/ok.png');
});
});
Or if all you're doing is assigning the .click()
handler, you can do it without the .each()
.
$('[id^="matchItem_"]').click(function(){
//...execute my script.php?urlparam=xx....;
$(this).find('img').attr('src','/admin/images/ok.png');
});
This is because most jQuery methods will automatically act on every element in the jQuery object.
(edited to add closing parenthesis in code)
精彩评论