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

実行結果

DOM要素:属性フィルター:[attribute=value]の使用例 | jQuery

チェックボックスの全選択・全解除、選択を逆にする

参照:jQuery Checkboxes: Select All, Select None, and Invert Selection

全選択全解除 | 選択を逆にする

グループ1

設置サンプルのソース

<!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>DOM要素:属性フィルター:[attribute=value]の使用例 | jQuery</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(){
            /* 全選択 */
            $("a[href='#select_all']").click( function() {
                $("#" + $(this).attr('rel') + " INPUT[type='checkbox']").attr('checked', true);
                return false;
            });
            /* 全解除 */
            $("a[href='#select_none']").click( function() {
                $("#" + $(this).attr('rel') + " INPUT[type='checkbox']").attr('checked', false);
                return false;
            });
            /* 選択を逆にする */
            $("a[href='#invert_selection']").click( function() {
                $("#" + $(this).attr('rel') + " INPUT[type='checkbox']").each( function() {
                    $(this).attr('checked', !$(this).attr('checked'));
                });
                return false;
            }); 
          });
      </script>
   <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>DOM要素:属性フィルター:[attribute=value]の使用例 | jQuery</h1>
         <h2>チェックボックスの全選択・全解除、選択を逆にする</h2>
         <p>参照:<a href='http://abeautifulsite.net/notebook/50'>jQuery Checkboxes: Select All, Select None, and Invert Selection</a></p>
<!-- CODE -->
         <p>
            <a rel="group_1" href="#select_all">全選択</a>
             | 
            <a rel="group_1" href="#select_none">全解除</a>
             | 
            <a rel="group_1" href="#invert_selection">選択を逆にする</a>
         </p>
         <fieldset id="group_1">
            <legend>グループ1</legend>
            <p>
                <label for="c1"><input type="checkbox" id="c1" name="numbers[]" value="0" /> 項目1
                <label for="c2"><input type="checkbox" id="c2" name="numbers[]" value="1" /> 項目2
                <label for="c3"><input type="checkbox" id="c3" name="numbers[]" value="2" /> 項目3
                <label for="c4"><input type="checkbox" id="c4" name="numbers[]" value="3" /> 項目4
                <label for="c5"><input type="checkbox" id="c5" name="numbers[]" value="4" /> 項目5
            </p>
            <p>
                <label for="c6"><input type="checkbox" id="c6" name="numbers[]" value="5" /> 項目6
                <label for="c7"><input type="checkbox" id="c7" name="numbers[]" value="6" /> 項目7
                <label for="c8"><input type="checkbox" id="c8" name="numbers[]" value="7" /> 項目8
                <label for="c9"><input type="checkbox" id="c9" name="numbers[]" value="8" /> 項目9
                <label for="c10"><input type="checkbox" id="c10" name="numbers[]" value="9" />項目10
            </p>
         </fieldset>
<!-- CODE / -->
      </div>
   </body>
</html>