admin管理员组文章数量:1023204
Using a single dynamic selector I have no problems:
var answer_id = <?php echo $answer_id; ?>;
$('#a_flag_' + answer_id).click(function(e) {
e.preventDefault();
//Ajax etc...
But if I add several dynamic selectors they do not work (ie, no errors on Firebug console, but also no action when clicked):
var answer_id = <?php echo $answer_id; ?>;
$('#a_flag_' + answer_id,'#a_ments_link_' + answer_id,'#a_best_answer_' + answer_id).click(function(e) {
e.preventDefault();
//Ajax etc...
Any ideas what I am doing wrong?
Using a single dynamic selector I have no problems:
var answer_id = <?php echo $answer_id; ?>;
$('#a_flag_' + answer_id).click(function(e) {
e.preventDefault();
//Ajax etc...
But if I add several dynamic selectors they do not work (ie, no errors on Firebug console, but also no action when clicked):
var answer_id = <?php echo $answer_id; ?>;
$('#a_flag_' + answer_id,'#a_ments_link_' + answer_id,'#a_best_answer_' + answer_id).click(function(e) {
e.preventDefault();
//Ajax etc...
Any ideas what I am doing wrong?
Share Improve this question asked Oct 30, 2011 at 22:15 pepepepe 9,90925 gold badges117 silver badges192 bronze badges3 Answers
Reset to default 5You need the mas inside the quoted constant strings.
$('#a_flag_' + answer_id + ', #a_ments_link_' + answer_id + ', #a_best_answer_' + answer_id).click(// ///
What you want to end up with is a string that looks like
"selector, selector, selector, ..."
so you need to concatenate a bunch of strings with mas.
Alternatively, you could build up your separate selectors in an array of strings and then ".join()" them with a ma separator (the parameter to ".join()").
You should put the a inside the string not outside and you have forgotten the plus.
$('#a_flag_' + answer_id + ', #a_ments_link_' + answer_id +',#a_best_answer_' + answer_id)
When it starts to look too plicated, it likely is. FWIW:
var selectors = [
'#a_flag_' + answer_id,
'#a_ments_link_' + answer_id
// etc.
]
$(selectors.join(", ")).click(...)
Happy coding.
Using a single dynamic selector I have no problems:
var answer_id = <?php echo $answer_id; ?>;
$('#a_flag_' + answer_id).click(function(e) {
e.preventDefault();
//Ajax etc...
But if I add several dynamic selectors they do not work (ie, no errors on Firebug console, but also no action when clicked):
var answer_id = <?php echo $answer_id; ?>;
$('#a_flag_' + answer_id,'#a_ments_link_' + answer_id,'#a_best_answer_' + answer_id).click(function(e) {
e.preventDefault();
//Ajax etc...
Any ideas what I am doing wrong?
Using a single dynamic selector I have no problems:
var answer_id = <?php echo $answer_id; ?>;
$('#a_flag_' + answer_id).click(function(e) {
e.preventDefault();
//Ajax etc...
But if I add several dynamic selectors they do not work (ie, no errors on Firebug console, but also no action when clicked):
var answer_id = <?php echo $answer_id; ?>;
$('#a_flag_' + answer_id,'#a_ments_link_' + answer_id,'#a_best_answer_' + answer_id).click(function(e) {
e.preventDefault();
//Ajax etc...
Any ideas what I am doing wrong?
Share Improve this question asked Oct 30, 2011 at 22:15 pepepepe 9,90925 gold badges117 silver badges192 bronze badges3 Answers
Reset to default 5You need the mas inside the quoted constant strings.
$('#a_flag_' + answer_id + ', #a_ments_link_' + answer_id + ', #a_best_answer_' + answer_id).click(// ///
What you want to end up with is a string that looks like
"selector, selector, selector, ..."
so you need to concatenate a bunch of strings with mas.
Alternatively, you could build up your separate selectors in an array of strings and then ".join()" them with a ma separator (the parameter to ".join()").
You should put the a inside the string not outside and you have forgotten the plus.
$('#a_flag_' + answer_id + ', #a_ments_link_' + answer_id +',#a_best_answer_' + answer_id)
When it starts to look too plicated, it likely is. FWIW:
var selectors = [
'#a_flag_' + answer_id,
'#a_ments_link_' + answer_id
// etc.
]
$(selectors.join(", ")).click(...)
Happy coding.
本文标签: javascriptMultiple dynamic selectors in jQuery using variablesStack Overflow
版权声明:本文标题:javascript - Multiple dynamic selectors in jQuery using variables - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745559655a2156084.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论