PHP & JavaScript Room :: 設置サンプル

実行結果

Progressive Enhancement: jQuery Rating | 設置サンプル

▼数値をクリックして投票すると、投票した数値が表示されます。

評価投票

投票した値:

設置サンプルのソース

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <meta http-equiv="Content-Language" content="ja" />
      <meta http-equiv="Content-Script-Type" content="text/javascript" />
      <meta http-equiv="Content-Style-Type" content="text/css" />
      <meta http-equiv="imagetoolbar" content="no" />
      <title>Progressive Enhancement: jQuery Rating | 設置サンプル</title>
      <link rel="stylesheet" type="text/css" href="/content/lib/global.css" />
      <!-- JS -->
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
      <script type="text/javascript">
         $(function(){
            /* 投票ボタンを非表示にする */
            $("input#btn_vote").hide();
            /* すべてのラジオボタンを非表示にする */
            $("form#rate_this input[type='radio']").hide();
            /* CSSでラベルをクリックしやすいようにする */
            $("form#rate_this label").addClass("selector");
            /* マウスオーバーした時ラベルをハイライト表示する */
            $("label.selector, #rating_scores a").hover(
               function(){ 
                  $(this).addClass("selector_h"); 
               }, function() { 
                  $(this).removeClass("selector_h");
            });
            /* クリックしたラベルの値を取得して表示 */
            $("form#rate_this input[type='radio']").click(function() { 
               data = $("form").serialize();
               $.ajax({
                  type: "POST",
                  url: "/content/demo/rating.php",
                  data:data,
                  success: function(evt, request, settings) {
                     $("#rating_result").text(evt);
                  },
                  error: function(msg) {
                     alert("投票に失敗しました");
                  }
               });
            });
         });
      </script>
      <!-- CSS -->
      <style type="text/css">
         #rating_scores { margin:0 0 10px 0; padding:10px; }
         #rating_scores label { margin:5px; padding:5px 7px; color: #000; text-transform:uppercase; cursor: pointer; font-size:1.2em; height:1.5em; font-weight:bold; }
         #rating_scores label.selector_h { background:#ff3366; color:#fff; }
         #rating_result { font-weight:bold; color:#ff3366; }
      </style>
   <link rel="stylesheet" type="text/css" href="/common/css/example.css"></head>
   <body id='example3' class='example'><div class="ads" style="margin:32px auto;text-align:center;"></div><h1 class='h'><a href='/'>PHP &amp; JavaScript Room</a> :: 設置サンプル</h1>
<h3 class='h'>実行結果</h3>
      <div id="wrap">
         <h1><a href='http://somedirection.com/2008/03/24/jquery-ratings-a-progressive-enhancement-approach/'>Progressive Enhancement: jQuery Rating</a> | 設置サンプル</h1>
         <p>▼数値をクリックして投票すると、投票した数値が表示されます。</p>
<!-- CODE -->
         <form id="rate_this" action="/content/demo/rating.php" method="post">
            <fieldset id="rating_scores">
               <legend>評価投票</legend>
               <label for="r1"><input id="r1" type="radio" value="1" name="rating" />1</label>
                 <label for="r2"><input id="r2" type="radio" value="2" name="rating" />2</label>
                 <label for="r3"><input id="r3" type="radio" value="3" name="rating" />3</label>
                 <label for="r4"><input id="r4" type="radio" value="4" name="rating" />4</label>
                 <label for="r5"><input id="r5" type="radio" value="5" name="rating" />5</label>
                 <label for="r6"><input id="r6" type="radio" value="6" name="rating" />6</label>
                 <label for="r7"><input id="r7" type="radio" value="7" name="rating" />7</label>
                 <label for="r8"><input id="r8" type="radio" value="8" name="rating" />8</label>
                 <label for="r9"><input id="r9" type="radio" value="9" name="rating" />9</label>
                 <label for="r10"><input id="r10" type="radio" value="10" name="rating" />10</label>
               <input type="submit" id="btn_vote" value="投票" />
            </fieldset>
            <p>投票した値:<span id="rating_result"></p>
         </form>
<!-- / CODE -->
      </div>
   </body>
</html>