drkj41932 2015-08-11 12:17
浏览 31
已采纳

从下拉菜单发送并设置3个选定值到JavaScript函数PHP

I have three drop down menus:

<?php 
        if(isset($som)) {

=======first drop down=======

              echo "<select id="."som-patch1"." class="."form-control".">";
                echo "<option value="."select".">Please Select SOM patch...</option>";


       for ($i=0; $i < count($som) ; $i++) { 
           echo '<option value="'.$som[$i]['som'].'">'.$som[$i]['som'].' </option>';
        }
        echo "</select>";

=======second drop down=======

                      echo "<select id="."som-patch1-month"." class="."form-control".">";
                echo "<option value="."select".">Please Select Month ...</option>";



       for ($i=0; $i < 12 ; $i++) {
           $month = date('F', strtotime('-'.$i.' month', strtotime(date('Y-m-01')))); 

           echo '<option value="'.$month.'">'.$month.'</option>';
        }
        echo "</select>";

=======third drop down=======



                echo "<select id="."som-patch1-year"." class="."form-control".">";
                echo "<option value="."select".">Please Select Year ...</option>";


       for ($i=0; $i < 12 ; $i++) {

           $year = date('Y', strtotime('-'.$i.' year', strtotime(date('Y-m-01'))));
           echo '<option value="'.$year.'">'.$year.'</option>';
        }
        echo "</select>";

      }        
    ?> 

I can successfully send the selected values to the following JavaScript function:

$('[id^=som-patch1]').on('change', function() {    
  alert( this.value ); 

});

The problem I have is:

I am unable to set the relevant values in the variables so that I could call the add_som_donut_chart_org_data() method. Also I need to make sure all THREE values have been selected before the function call.

I have tried the following:

    $('[id^=som-patch1]').on('change', function() {

      var som = null;
      var month = null;
      var year = null;
    if (id=='som-patch1'){
som = this.value;
}
      alert( som ); 


    });

But this doesn't set the som value. The desired outcome should be:

if (som !== null && month !== null && year !== null) {add_som_donut_chart_org_data(som,month,year)}

Thanks in advance.

UPDATE: The following code resolved the issue

$('[id^=som-patch1]').on('change', function() {

  var id = $(this).attr("id");

  if (id == 'som-patch1') {
           som = this.value;
  }
  if (id == 'som-patch1-month') {
           month = this.value;
  }
  if (id == 'som-patch1-year') {
           year = this.value;
  }


    if (som !== null && month !== null && year !== null) {

      add_som_donut_chart_org_data(som,month,year);

    }

});
  • 写回答

1条回答 默认 最新

  • douduocuima61392 2015-08-13 04:39
    关注

    As per your current code condition if (id=='som-patch1') would never be true, because id is not defined yet. Hence it will not set som variable. The reason is very basic that you have not defined id variable in your code. Before your condition use following code:

    var id = $(this).attr("id");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理