[英]JQuery alternate div content and AJAX post url on each click
I have some JQuery handling a div click. 我有一些JQuery处理div点击。 If the div is clicked once it hides itself and shows another div (and posts to a php script in the background).
如果div被单击,它将隐藏自身并显示另一个div(并在后台发布到php脚本)。 If it is then clicked again, it's supposed to hide the new div, show the new one again, and post more data to a different script, and so on.
如果再次单击它,则应该隐藏新的div,再次显示新的div,然后将更多数据发布到其他脚本中,依此类推。
It changes the first time (.f hides, .uf shows) but when I click '.uf' nothing happens. 它第一次更改(.f隐藏,.uf显示),但是当我单击“ .uf”时,什么也没有发生。
JQuery: jQuery的:
if($('.f').is(":visible")) {
$('#follow').click(function() {
var to_follow = $('.name').attr('id');
$.ajax({
type: "POST",
url: "/bin/rel_p.php",
data: "y=" + to_follow,
success: function() {
$('.f').hide();
$('.uf').show();
}
});
});
}
else {
$('#follow').click(function() {
var to_unfollow = $('.name').attr('id');
$.ajax({
type: "POST",
url: "/bin/relu_p.php",
data: "y=" + to_follow,
success: function() {
$('.uf').hide();
$('.f').show();
}
});
});
}
HTML: HTML:
<div id="follow" class="f">
<img src="img/icons/Follow.png">
<h1>Follow</h1>
</div>
<div id="follow" class="uf" style="display:none;">
<img src="img/icons/Unfollow.png">
<h1>Unfollow</h1>
</div>
use a class if you intend to use the selector twice, and don't write the same code twice unless you have to: 如果您打算两次使用选择器,请使用一个类,并且不要两次编写相同的代码,除非您必须:
$('.follow').on('click', function() {
var is_f = $('.f').is(":visible"),
to_follow = $('.name').attr('id'),
give_me_an_u = is_f?'':'u';
$.ajax({
type: "POST",
url: "/bin/rel" + give_me_an_u + "_p.php",
data: {y : to_follow}
}).done(function() {
$('.f, .uf').toggle();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.