我正在構建一個簡單的投票事項,它會出現在大學網站的邊欄中。它的工作方式很簡單。你挑選你喜歡的人就是這樣。它的結構如下。有一個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';
}
?>