Udd*_*ers 1 javascript ajax jquery jquery-selectors
我有一个按钮,根据类值应该发送不同的值到POST但是发生了什么它只运行一个条件,无论我做什么我只能得到它发送POST方法=添加数据.
这是我的代码
$("a.navlink").click(function (ev) {
$(this).removeClass("active");
var url = $(this).attr("href")
ev.preventDefault();
if($('a.navlink:not(.active)')) {
$.ajax ({
url: url,
type: "POST",
data: "method=add",
success : function (html) {
$('#accordion').accordion('destroy');
$("#accordion").append(html);
$('#accordion').accordion({
active: 0,
header:'h2.Blog'
});
//alert(accordion())
}
});
}
if ($(this).hasClass("active")) {
// $(this).addClass('active')
$.ajax ({
url: url,
type: "POST",
data: "method=delete",
success : function (html) {
alert("hello")
$(this).removeClass('active')
//alert(accordion())
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
基本上我需要的是,如果单击按钮时它会获得一个名为active的类,并且ajax运行,当再次单击它时,需要删除该类并运行ajax,这可能吗?
你在第一个if子句中现在基本上做的是你要检查$('a.navlink:not(.active)')是真还是假 - $()总是返回一个计算结果为true的对象.你也应该对它做一个hasClass.而你之前正在删除"活跃"类if,那么你以后如何比较呢?
目前还不清楚你要做什么,但你想要的可能是这样的:
$("a.navlink").click(function(ev) {
var url = $(this).attr("href")
ev.preventDefault();
// If the link is not active, run 'add':
if(!$(this).hasClass('active')) {
$.ajax ({
url: url,
type: "POST",
data: "method=add",
success: function (html) {
$('#accordion').accordion('destroy');
$("#accordion").append(html);
$('#accordion').accordion({
active: 0,
header:'h2.Blog'
});
}
});
// Mark it as active
$(this).addClass('active');
// If the link is active, run 'delete':
} else {
$.ajax ({
url: url,
type: "POST",
data: "method=delete",
success: function (html) {
alert("hello")
}
});
// Unmark it.
$(this).removeClass('active');
}
});
Run Code Online (Sandbox Code Playgroud)
那段代码的目的对我来说没有多大意义,所以告诉我,如果我理解你错了.
| 归档时间: |
|
| 查看次数: |
526 次 |
| 最近记录: |