当我按下具有相同类名的任何按钮时,它们都会激活相同的AJAX请求,因此如何针对具有相同类的特定按钮单独激活请求
名称我在此使用 此 关键字,但我无法使其正常使用,所以我退回了这个关键字。为了简单起见,我构建了这样的代码。
这是代码
的index.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('.sendRequest').click(function(){
$.ajax({
type: 'POST',
url: 'x.php',
success: function(result){
$('.responseContainer').html(result)
}
});
});
});
</script>
<button class='sendRequest'>Go</button>
<div class='responseContainer'></div>
<br>
<button class='sendRequest'>Go</button>
<div class='responseContainer'></div>
<br>
<button class='sendRequest' >Go</button>
<div class='responseContainer'></div>
x.php
<h1>The Ajax Request was sent by this button</h1>
答案 0 :(得分:0)
您必须找到与class responseContainer匹配的下一个兄弟。
这就是你需要的:
$(document).ready(function(){
$('.sendRequest').click(function(){
var $container = $(this).next('.responseContainer');
$.ajax({
type: 'POST',
url: 'x.php',
success: function(result){
$container.html(result)
}
});
});
});
});