2013-03-29 17 views
0

我想對我的選擇啓動觸發,但似乎沒有什麼工作:觸發的jQuery並不劉

這是我的選擇:

<select id="sel_annee"> 
     <option value="<?php echo (date('Y')-1); ?>"><?php echo (date('Y')-1); ?></option> 
     <option value="<?php echo date('Y'); ?>" selected='selected'><?php echo date('Y'); ?></option> 
     <option value="<?php echo (date('Y')+1); ?>"><?php echo date('Y')+1; ?></option> 
    </select> 

這是我的觸發:

$(document).ready(function(){ 

      $('#sel_annee').trigger("change"); 

,這是功能

$('#sel_annee').change(function() 
      { 
       alert("here");    
       $('.month').hide(); 
       $('.month:first').show(); 
       $('.months a:first').addClass('active'); 

          $.ajax({ 
          type: 'post', 
          url: 'changer_annee.php', 
          data: 
          { 
           year:$('#sel_annee').val() 
          }, 
          dataType: 'text', 

          success: function(retour_php) 
          { 
           $('#month1').html('');   
           //console.log(retour_php); 
           var arr = retour_php.split('#'); 

           //console.log(retour_php); 

           for(i= 1; i<=12;i++) 
           { 
            $('#month'+i).html(arr[i-1]); 
            //console.log($('#month'+i)); 
            //console.log(arr[i]); 
           } 

           //$('.month').hide(); 
           $('.month:first').show(); 
           $('.months a').removeClass('active'); 
           $('.months a:first').addClass('active'); 
           //$('.months a:first').addClass('active'); 


          }, 
          error: function(retour_php) 
          { 
           alert("pas ok"); 
           alert(retour_php); 
          } 

          } 
         ); 
      }); 

此警報不起作用:

$('#sel_annee').change(function() 
       { 
        alert("here"); 

我覺得我做了所有我不得不這樣做,但是觸發不起作用。

有什麼想法?提前致謝。

回答

1

我相信你已經試圖觸發器change甚至在你已經附加了change處理程序。

而應該做的:

$(document).ready(function(){ 

    // bind the change handler 
    $('#sel_annee').change(function() { 
     ... 
    }); 

    // now trigger it 
    $('#sel_annee').trigger("change"); 
}); 
+0

大,它的偉大工程!不幸的是,我不能給你一個額外的觀點,我沒有足夠的觀點來做到這一點。有一個優秀的W.E. – beegees

+0

@beegees - 這不是問題。很高興幫助。 :) – techfoobar

0

請確保您的文檔準備好處理程序代碼,您首先連接更改處理,然後觸發change事件。

在你的代碼中一切都很完美,唯一我認爲可能會出錯的是試圖在附加一個函數之前觸發事件。

0

嘗試這樣:

jQuery(function(){ 
    $('#sel_annee').change(function(){ 
    alert('changed'); 
    }); 
    $('#sel_annee').trigger("change"); 
});