prototype.js を使ってフォーム入力検証。
checkbox と radio のチェック。
Field.present('ID')
が IE で使えないみたい。
で関数を作ってみた。
function checkFormChecked (type, name, count) { var bool = false; var find = 0; if (!count) count = 1; var elements = $('form ID').getInputs(type, name); elements.each( function (obj) { if ($F(obj)) find++; if (find >= count) { bool = true; return; } } ); return bool; } |
フォーム
<!--radio--> <input type="radio" id="q1-0" name="q1" value="ある" /><label for="q1-0">ある</label> <input type="radio" id="q1-1" name="q1" value="ない" /><label for="q1-1">ない</label> <!--checkbox--> <input type="checkbox" id="q2-1" name="q2[]" value="1.りんご" /><label for="q2-1">1.りんご</label> <input type="checkbox" id="q2-2" name="q2[]" value="2.みかん" /><label for="q2-2">2.みかん</label> <input type="checkbox" id="q2-3" name="q2[]" value="3.いちご" /><label for="q2-3">3.いちご</label> <input type="checkbox" id="q2-4" name="q2[]" value="4.すいか" /><label for="q2-4">4.すいか</label>> |
使い方は
checkFormChecked("radio","q1"); // true or false checkFormChecked("checkbox","q2[]"); // true or false |
prototype.js の $('form ID').getInputs(type, name)
を使って対象データを取出して each
を使って処理をする。
Ver. 1.6 からは break, continue
は return
にしてネらしい。