动态下拉选项数据不存储在数据库中,而仅存储索引号

时间:2018-08-04 07:28:04

标签: javascript php html

我在使用HTML表单的JavaScript动态下拉列表。我还与数据库连接以存储所有值。它完美地存储除下拉列表值以外的所有值,但代替值,它存储索引值。我使用javascript数组方法进行下拉

    <select id="service" required name="service" onchange="ChangeServiceList()"> 
        <option value="">-- Select --</option> 
        <option value="web">Web Design</option> 
        <option value="civil">Civil Services</option> 
        <option value="electrical">Electrical Services</option> 
    </select> 

    <select id="submenu" required name="category" value=""></select>

javascript代码:

    <script>
      var serviceAndLists = {};
      serviceAndLists['web'] = ['-- select --','Static', 'Dynamic', 'E-comeerce'];
      serviceAndLists['civil'] = ['-- select --','c_type1', 'c_type2', 'c_type3'];
      serviceAndLists['electrical'] = ['-- select --','e_type1', 'e_type2', 'e_type3'];

      function ChangeServiceList() {
         var serviceList = document.getElementById("service");
         var modelList = document.getElementById("submenu");
         var selService = serviceList.options[serviceList.selectedIndex].value;
         while (modelList.options.length) {
         modelList.remove(0);
       }
       var services = serviceAndLists[selService];
       if (services) {
         var i;
         for (i = 0; i < services.length; i++) {
            var service = new Option(services[i], i);
            modelList.options.add(service);
           }
          }
         } 
       </script>

HTML表单工作正常...但是在数据库中存储一个值,它仅存储索引号... 请帮帮我。

1 个答案:

答案 0 :(得分:0)

仅说明一下,当您创建一个新的Option()时,第二个参数将定义该特定选项的值。有关更多信息,请参见here

<script>
  var serviceAndLists = {};
  serviceAndLists['web'] = ['-- select --','Static', 'Dynamic', 'E-comeerce'];
  serviceAndLists['civil'] = ['-- select --','c_type1', 'c_type2', 'c_type3'];
  serviceAndLists['electrical'] = ['-- select --','e_type1', 'e_type2', 'e_type3'];

  function ChangeServiceList() {
     var serviceList = document.getElementById("service");
     var modelList = document.getElementById("submenu");
     var selService = serviceList.options[serviceList.selectedIndex].value;
     while (modelList.options.length) {
     modelList.remove(0);
   }
   var services = serviceAndLists[selService];
   if (services) {
     var i;
     for (i = 0; i < services.length; i++) {
        var service = new Option(services[i], services[i]);
        modelList.options.add(service);
        modelList.options.add(service);
       }
      }
     } 
</script>