2015-11-23 31 views
1

我有我打電話onclick一個dataSend AJAX功能,但它是沒有得到調用。 我檢查了它在我的瀏覽器督察並單擊處理程序連接到它,但,當我把一個斷點在函數中使用調試器,它永遠不會到達那裏。的onclick功能沒有得到所謂的

PHP/HTML摘錄(RAW)

<td onclick="dataSend('<?php echo $year;?>','12','<?php echo $rs->StudentId;?>');"><?php echo $count12[$rs->StudentId]."/248"; ?></td> 

現在,這是混亂的,因爲PHP的,這裏是它的外觀是在瀏覽器中運行後。

在瀏覽器中運行

<td class=" " onclick="dataSend('2015','02','186');">/224</td> 

AJAX片段(功能)後

function dataSend(year, month, studentid) { 

    parameters = 'StudentId='+studentid+'&Month='+month+'&Year='+year; 
    if(window.XMLHttpRequest) { 
     xmlhttp = new XMLHttpRequest(); 
     } else { 
     xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); 
    } 
    xmlhttp.onreadystatechange = function() { 
     if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { 

      document.getElementById('iframe').innerHTML = xmlhttp.responseText; 
     } 
    } 
    xmlhttp.open('POST', 'attendancestu.php', true); 
    xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); 
    xmlhttp.send(parameters); 
} 

FYI - 這個功能是不是任何其他功能或觸發器內。它甚至不是裏面$(document).ready();

我寫的代碼這樣幾百次,但我似乎無法人物出入這裏的問題

+0

對不起,我想它和它的工作,你有什麼其他的控制檯輸出或者可能是阻止瀏覽器擴展? – swidmann

+0

@swidmann - 不,我不這麼認爲。你認爲它背後的原因可能不起作用? –

+0

你可以發佈你的整個生成的HTML代碼? –

回答

0

您也可以重寫此另一個結合水平,這也使得結構您的表格方式更易於閱讀。

<td class="send-data" data-year="<?= $year ?>" data-month="12" data-student-id="<?= $rs->StudentId ?>"><?= $count12[$rs->StudentId]."/248"; ?></td> 

<script> 
$(document).on('click', '.send-data', function(e) { 
    //stop everything what would happen on click of td 
    e.stopPropagation(); 
    e.preventDefault(); 


    // keep track of the clicked item 
    _this = $(this); 

    studentId = _this.data('student-id'); 
    month = _this.data('month'); 
    year = _this.data('year'); 

    parameters = 'StudentId='+studentId+'&Month='+month+'&Year='+year; 
    if(window.XMLHttpRequest) { 
     xmlhttp = new XMLHttpRequest(); 
    } else { 
     xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); 
    } 
    xmlhttp.onreadystatechange = function() { 
     if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
      document.getElementById('iframe').innerHTML = xmlhttp.responseText; 
     } 
    } 
    xmlhttp.open('POST', 'attendancestu.php', true); 
    xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); 
    xmlhttp.send(parameters); 
}); 

0

請HTML之前加載JS。手段把你的JS文件中

也改變參數如下:

parameters = window.location.href + '/StudentId='+studentid+'&Month='+month+'&Year='+year; 

並嘗試LIVE運行或本地主機