我正在构建一个简单的投票事项,它会出现在大学网站的边栏中。它的工作方式很简单。你挑选你喜欢的人就是这样。它的结构如下。有一个heading
,sub-heading
然后candidates
。每个candidate
旁边是like
链接。用户为候选人投票后删除所有其他'投票'链接
我被困在这里:当用户点击一个like
链接,如果demo.php
发生的事情是成功的,那么所需要的所有其他like
链接为sub-heading
被取出,因此用户不能别人下投票给任何人那sub-heading
了。
这样做是如何做到这一切都是这样构建的。如果将</div>
的id=h2
移到like
以下,它会让我觉得更容易。
由于刚建好,我愿意实施变更。
这里是我的demo.htm
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Side bar voting thingy</title>
<script type="text/javascript" src="http://localhost/site/scripts/jQueryCore.js"></script>
<script type="text/javascript">
$(function() {
$(".like").click(function() {
var hasLike = $(this).data("id");
var data = 'id='+hasLike;
console.log($(this).data('id'));
if(hasLike) {
// ajax call
$.ajax({
type:"GET",
url:"demo.php",
data:data,
beforeSend:function(html){
// We'll think of something to do here
},
success: function(page_data){
// Remove the remaining like links. How?
$('.like[data-id="'+page_data+'"]').append(page_data);
},
error: function(page_data){
$("#errors").empty();
$("#errors").fadeIn(200);
$("#errors").append('Screwed up!');
},
});
}
return false;
});
});
</script>
</head>
<body>
<div id="container">
<div id="h1" data-id="1">Teachers</div>
<div id="h2" data-id="2">Who is your favorite Math teacher?</div>
<div>* Homer Simpson   <span id="h3" class="like" data-id="3" data-sec="2">Like</span></div>
<div>* Elmer Fudd   <span id="h3" class="like" data-id="4" data-sec="2">Like</span></div>
<div>* Bugs Bunny   <span id="h3" class="like" data-id="5" data-sec="2">Like</span></div>
<div>* Obelix   <span id="h3" class="like" data-id="6" data-sec="2">Like</span></div>
<div>* Mojo Jojo   <span id="h3" class="like" data-id="7" data-sec="2">Like</span></div>
<br>
<div id="h1" data-id="8">Restaurants</div>
<div id="h2" data-id="9">Which is your favourtie restaurant in town?</div>
<div>* McDonalds   <span id="h3" class="like" data-id="10" data-sec="9">Like</span></div>
<div>* KFC   <span id="h3" class="like" data-id="11" data-sec="9">Like</span></div>
<div>* The Heart Attack Grill   <span id="h3" class="like" data-id="12" data-sec="9">Like</span></div>
<div>* In-n-Out   <span id="h3" class="like" data-id="13" data-sec="9">Like</span></div>
<div>* Popeye's   <span id="h3" class="like" data-id="14" data-sec="9">Like</span></div>
<div id="errors" style="display:none;"></div>
</div>
</body>
</html>
这里的demo.php (没有什么在这里现在)
<?php
if(isset($_GET['id'])){
echo $_GET['id'];
} else {
echo 'Error! Id not found';
}
?>