Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用id';s_Javascript_Jquery_Jquery Selectors - Fatal编程技术网

Javascript 使用id';s

Javascript 使用id';s,javascript,jquery,jquery-selectors,Javascript,Jquery,Jquery Selectors,我有一个id数组,如下所示: answerIds = ['0', '5', '20'] 我希望创建如下所示的jquery选择器: $('.answer-reason[data-choice-id="' + answerId + '"]'); 其中answerId是answerId中的值 换句话说,我希望它返回以下元素: .answer-reason[data-choice-id="0"] .answer-reason[data-choice-id="5"] .answer-reason[da

我有一个id数组,如下所示:

answerIds = ['0', '5', '20']
我希望创建如下所示的jquery选择器:

$('.answer-reason[data-choice-id="' + answerId + '"]');
其中
answerId
answerId
中的值

换句话说,我希望它返回以下元素:

.answer-reason[data-choice-id="0"]
.answer-reason[data-choice-id="5"]
.answer-reason[data-choice-id="20"]
我该怎么做

注意,我希望最终结果是jQuery对象

换句话说,我应该能够做到:

$output.on('click', function() { ... })`
比如说

var el = [];
$.each(answerIds, function(index, element){ 
  el.push($('.answer-reason[data-choice-id="' + element + '"]'));
});

这应该可以。根据您的示例,变量el将包含所有三个元素。

查看
中的
循环。但是
el
是元素的jQuery数组吗?i、 例如,我可以在('click',function(){…})
上执行
el.on('click',function(){…})
吗。我称这个变量为‘el’,只是为了清楚起见。您可以为变量使用不同的名称。@YahyaUddin是的,您可以像上述情况一样使用变量“el”创建事件。