admin管理员组文章数量:1400117
I currently have four change()
events that fire exactly the same code (see below), so I'm wondering if there is a way of grouping these events in one line to save reapeating code four times?
Thanks.
/** Check for a change to any of the search dropdowns */
$('#job-roles').change(function(e){
letter = $_GET('staff_search');
initiate_staff_search(letter, '<?php echo $staff_search_nonce; ?>', '<?php echo $name_search_type; ?>')
});
$('#ind-services').change(function(e){
letter = $_GET('staff_search');
initiate_staff_search(letter, '<?php echo $staff_search_nonce; ?>', '<?php echo $name_search_type; ?>')
});
$('#bus-services').change(function(e){
letter = $_GET('staff_search');
initiate_staff_search(letter, '<?php echo $staff_search_nonce; ?>', '<?php echo $name_search_type; ?>')
});
$('#indi-services').change(function(e){
letter = $_GET('staff_search');
initiate_staff_search(letter, '<?php echo $staff_search_nonce; ?>', '<?php echo $name_search_type; ?>')
});
I currently have four change()
events that fire exactly the same code (see below), so I'm wondering if there is a way of grouping these events in one line to save reapeating code four times?
Thanks.
/** Check for a change to any of the search dropdowns */
$('#job-roles').change(function(e){
letter = $_GET('staff_search');
initiate_staff_search(letter, '<?php echo $staff_search_nonce; ?>', '<?php echo $name_search_type; ?>')
});
$('#ind-services').change(function(e){
letter = $_GET('staff_search');
initiate_staff_search(letter, '<?php echo $staff_search_nonce; ?>', '<?php echo $name_search_type; ?>')
});
$('#bus-services').change(function(e){
letter = $_GET('staff_search');
initiate_staff_search(letter, '<?php echo $staff_search_nonce; ?>', '<?php echo $name_search_type; ?>')
});
$('#indi-services').change(function(e){
letter = $_GET('staff_search');
initiate_staff_search(letter, '<?php echo $staff_search_nonce; ?>', '<?php echo $name_search_type; ?>')
});
Share
Improve this question
edited Jan 28, 2012 at 16:43
PeeHaa
72.8k60 gold badges194 silver badges264 bronze badges
asked Nov 4, 2011 at 16:12
David GardDavid Gard
12.1k42 gold badges128 silver badges263 bronze badges
7
-
3
letter = $_GET('staff_search');
??? – Matt Commented Nov 4, 2011 at 16:16 - +1 @Matt hahaha I didn't spot that !!!! – Manse Commented Nov 4, 2011 at 16:22
- @Matt - What's the problem with that code? – David Gard Commented Nov 4, 2011 at 16:35
-
@DavidGard: If thats supposed to be a variable, you should prefix the statement with
var
. What is$_GET['staff_search']
? You mean"<?php echo addcslashes($_GET['staff_search'], '"'); ?>"
(PHP is rusty.. but hope you get the gist). – Matt Commented Nov 4, 2011 at 16:43 -
@Matt - Yes, I should have
var
before the statement.$_GET
is a function. I add the letter that is being searched for to the end of the url (i.e.#?staff_search=m
) and then use this to get the letter. I need to have the letter in the URI as each time a user changes a search criteria the search is automatically run. This is the best way I could think to do it, and$_GET()
is just fortable because I use PHP. – David Gard Commented Nov 4, 2011 at 16:47
4 Answers
Reset to default 5$('#job-roles, #indi-services, #ind-services, #bus-services').change(function(e){
letter = $_GET('staff_search');
initiate_staff_search(letter, '<?php echo $staff_search_nonce; ?>', '<?php echo $name_search_type; ?>')
});
Option 1: give them all the same class and attach the event to all of them $('.someClass').change(...)
Option 2: Group the event: $('#job-roles, #ind-services, ... ').change(...)
Use a class ?
Give all the elements a class of say doSomething
then use the following code to implement the listener:
$('.doSomething').change(function(e){
letter = $_GET('staff_search');
initiate_staff_search(letter, '<?php echo $staff_search_nonce; ?>', '<?php echo $name_search_type; ?>')
});
Another option, in addition to the excellent ones already described, is simply:
(function(){
function doCoolStuff(){
letter = $_GET('staff_search');
initiate_staff_search(letter, '<?php echo $staff_search_nonce; ?>', '<?php echo $name_search_type; ?>')
}
$('#job-roles').change(function(e){
doCoolStuff();
});
$('#ind-services').change(function(e){
doCoolStuff();
});
$('#bus-services').change(function(e){
doCoolStuff();
});
$('#indi-services').change(function(e){
doCoolStuff();
});
}());
本文标签: javascriptGroup JS eventsStack Overflow
版权声明:本文标题:javascript - Group JS events - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744207034a2595239.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论