drkj41932 2015-08-11 04: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-12 20: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");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?