我想要同时单击具有不同id(或class =“ respond”)的按钮和具有公共ID(#resButt)(在弹出窗口中)的按钮),并为所有id执行ajaxResponse()...
sealguyue 回答:有没有办法一次遍历不同的id和响应json(使用for循环或每个循环)和console.log(json键:值对)?
尝试这样的事情:
function ajaxResponse() {
alert('This triggers the ajaxResponse function');
}
$('.respond').click(function(e){
console.log(e.target.id);
$('#respButt').parent().show();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class="respond" href="#inline_content" id="52273">Respond</a>
<a class="respond" href="#inline_content" id="52270">Respond</a>
<a class="respond" href="#inline_content" id="52256">Respond</a>
<div style="display:none;border:1px solid black;background: lightgreen">
<p>Form goes here</p>
<button id="respButt" name="respBtn" onclick="ajaxResponse()">Send My Response</button>
</div>
您可以使用JQuery将onclick事件添加到“响应”类的每个元素上,在这种情况下,它将把其id记录到控制台并打开包含按钮和表单的div。如果您需要按钮成为表单的一部分,只需更改此行:
$('#respButt').parent().show();
包括“ parent()”的当前编号。
希望这会有所帮助。
,
function ajaxResponse() {
alert('This triggers the ajaxResponse function');
$('.respond').each(function(e){
$(this).trigger('click');
});
}
$('.respond').click(function(e){
alert(e.target.id);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class="respond" href="#inline_content" id="52273">Respond</a>
<a class="respond" href="#inline_content" id="52270">Respond</a>
<a class="respond" href="#inline_content" id="52256">Respond</a>
<div style="border:1px solid black;background: lightgreen">
<p>Form goes here</p>
<button id="respButt" name="respBtn" onclick="ajaxResponse()">Send My Response</button>
</div>