- <form name="update_data" action="update_data.php">
- 暱名:<input type="text" name="nick" value="<?=$nick?>">
- 興趣:
- <input type="checkbox" name="ints[]" value="動畫">動畫
- <input type="checkbox" name="ints[]" value="閱讀">閱讀
- <input type="checkbox" name="ints[]" value="電影">電影
- <input type="checkbox" name="ints[]" value="漫畫">漫畫
- <input type="checkbox" name="ints[]" value="籃球">籃球
- <input type="checkbox" name="ints[]" value="健走">健走
- <input type="submit" value="送出修改">
- </form>
- <script>
- var checkvalue = "<?=$ints?>";
- var theform = document.update_data.elements;
- for(i=0;i<theform.length;i++) {
- //迴圈檢查型別為checkbox且名稱為ints[]的項目,是否有興趣值符合
- if(theform[i].type == "checkbox" && theform[i].name.indexOf('ints[]')!= "-1" && checkvalue.indexOf(theform[i].value)!=-1){
- theform[i].checked = true;
- }
- }
- </script>
舊式寫法
- <?php $test = 'B'; ?>
- <form name="test1">
- <select name="test2">
- <option value="A">A</option>
- <option value="B">B</option>
- <option value="C">C</option>
- </select>
- </form>
- <script>document.test1.test2.value = '<?=$test?>';</script>