当我按下其中一个按钮时,我有一组带按钮的类。所有课程都会发生变化。但我希望它只能在活跃的div上。
我知道我应该使用$ this。但我无法管理它。有什么想法吗?
function close(){
$('.close').click(function() {
$.ajax({
url: "php/functions.php",
type: "POST",
data: "php",
success: function(html) {
var note = $(".note").html(html);
}
});
return false;
});
}
php文件只包含一个echo
<?php echo "Heloo" ?>
html
<div class="note">
<div class="close"></div>
</div>
<div class="note">
<div class="close"></div>
</div>
答案 0 :(得分:1)
你想要的是这个 -
$('.close').click(function() {
var $this = $(this);
$.ajax({
url: "php/functions.php",
type: "POST",
data: "php",
success: function(html) {
$this.closest(".note").html(html);
//or
$this.parent().html(html);
//or
$this.parent().html(html);
}
});
});
但是,这会覆盖你的关闭div。如果你想保持你的近距离 -
HTML:
<div class="notewrap">
<div class="note"></div>
<div class="close"></div>
</div>
<div class="notewrap">
<div class="note"></div>
<div class="close"></div>
</div>
Javascript(仅使用回调中提供的选项之一):
$('.close').click(function() {
var $this = $(this);
$.ajax({
url: "php/functions.php",
type: "POST",
data: "php",
success: function(html) {
//Option 1 - will work even if you change the html structure a fair bit
$this.parents(".notewrap").find(".note").html(html);
//Option 2 - will only work if your structure remains very similar
$this.siblings(".note").html(html);
//Option 3
$this.parent().find(".note").html(html);
}
});
});
答案 1 :(得分:0)
$('.close').click(function() {
$(this).addClass("SOMETHING"); <--- like this
$.ajax({
url: "php/functions.php",
type: "POST",
data: "php",
success: function(html) {
var note = $(".note").html(html);
}
});
答案 2 :(得分:0)
如果我正确理解您的问题,您只是想更改在ajax success
回调中点击的按钮?这在回调中不起作用,因为它不会返回按钮(它会返回我认为的窗口。不确定)
你必须在ajax帖子之前获得对该按钮的引用。试试这个:
$('.close').click(function() {
var self = $(this);
$.ajax({
url: "php/functions.php",
type: "POST",
data: "php",
success: function(html) {
//Now you can do whatever you want with 'self', which is the button that was clicked
self.addClass('myclasstoadd');
var note = $(".note").html(html);
}
});