お世話になります
htmlとjavascriptで 最初に選んだselectタグの内容により次のselectタグの中身が変化するものがあります。
今回その選択が2つあるのですが、選択肢の中身が全く同じです
html
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2<html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 5 <meta content="86400" http-equiv="Expires" > 6 7 <script type = "text/javascript"> 8 <!-- 9 function select1() 10 { 11 var select1 = document.forms.formName.selectName1; 12 var select2 = document.forms.formName.selectName2; 13 14 select2.options.length = 0; 15 16 if (select1.options[select1.selectedIndex].value == "TV") 17 { 18 select2.options[0] = new Option("SONY"); 19 select2.options[1] = new Option("PANA"); 20 select2.options[2] = new Option("TOSHI"); 21 } 22 23 else if (select1.options[select1.selectedIndex].value == "PHONE") 24 { 25 select2.options[0] = new Option("APPLE"); 26 select2.options[1] = new Option("GOOGLE"); 27 select2.options[2] = new Option("ANDOROID"); 28 } 29 30 } 31 32 function select2() 33 { 34 var select1 = document.forms.formName.selectName3; 35 var select2 = document.forms.formName.selectName4; 36 37 select2.options.length = 0; 38 39 if (select1.options[select1.selectedIndex].value == "TV") 40 { 41 select2.options[0] = new Option("SONY"); 42 select2.options[1] = new Option("PANA"); 43 select2.options[2] = new Option("TOSHI"); 44 } 45 46 else if (select1.options[select1.selectedIndex].value == "PHONE") 47 { 48 select2.options[0] = new Option("APPLE"); 49 select2.options[1] = new Option("GOOGLE"); 50 select2.options[2] = new Option("ANDOROID"); 51 } 52 53 } 54 55 //--> 56 </script> 57</head> 58 59<body bgcolor onLoad="select1();select2()"> 60 <form name="formName" method="post" action="./pathToProgramFile"> 61 62 <select name = "selectName1" onChange="select1()"> 63 <option value = "TV">TV</option> 64 <option value = "PHONE">PHONE</option> 65 66 </select> 67 68 <select name = "selectName2"> 69 </select> 70 71 <select name = "selectName3" onChange="select2()"> 72 <option value = "TV">TV</option> 73 <option value = "PHONE">PHONE</option> 74 75 </select> 76 77 <select name = "selectName4"> 78 </select> 79 80 </body>
この場合 select()1とselect()2 でやっていることはほぼ同じなのですが、これをまとめる事とかは可能でしょうか?
したい事として、関数の中の if の所(選択肢2個めの方)だけを別のファイルに書いてincludeするなど、何か方法を教えて頂きたいと思います
よろしくお願いします
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/07/25 03:12