読んでくれてありがとう。
私は改善しようとしているので、改善するためのサンプルプロジェクトを行っています。シンプルにしました。投票システム。PHPで表示されるコンテンツの数があり、それぞれに賛成または反対の投票ボタンがあります。ちなみに私はtwitterのブートストラップを使っています。
これはhtmlコードです:
<html>
<button type="submit" class="btn btn-mini upvote" id="'.$data['ContentID'].'">
<i class="icon-arrow-up"></i>
</button>
<span id="voteresponse">'.$data['VoteSum'].'</span>
<button type="submit" class="btn btn-mini downvote" id="'.$data['ContentID'].'">
<i class="icon-arrow-down"></i>
</button>
</html>
class="upvote"
問題は、他のすべてのボタンが同じことをするボタンをなめるときです。php によって入力されるデータには多くのデータがあるためです。
これは私のJavaScriptです。
<script>
jQuery(document).ready(function($){
$('.upvote').click( function(event) {
event.preventDefault();
$("#voteresponse").html('');
var voteID = $(".upvote").first().attr("id");
$.ajax({
url: "/ajax.php?Page=votetitle",
type: "post",
dataType: "json",
data: {id : voteID},
success: function(data, textStatus){
if(data.success == 'true'){
$('#voteresponse').html(data.message);
return true;
}else{
$('#voteresponse').popover({
title: 'Hata!',
content: data.message
});
}
},
error:function(){
$('#voteresponse').popover({
title: 'error!',
content: 'Server error'
});
}
});
});
});
</script>
PHPコードは通常のデータベースリクエストです。
<?php
if ( $_SESSION['UserID'] <1 )
die( print_r(json_encode(array('success'=>'false', 'message'=>'error for user!'))) );
print_r(json_encode(array('success'=>'true', 'message'=>$_POST['id'])))
?>
ここでアクションを見ることができます。それらの 1 つをクリックすると、他のすべての上矢印が同じことを行います。また、このアプローチは正しいですか?
ありがとう。よろしくお願いします