2015-07-20 26 views
0

我嘗試將按鈕值(整數)作爲頁碼發送到ajax函數,然後發送到php文件,但無法使其工作。按鈕是:如何向ajax發送按鈕值

<form method="get" onchange = PageFunction(this.value)> 
<button class = 'pages' type='button' onchange = 'PageFunction(this.value)' value="1">1</button> 
<button class = 'pages' type='button' onchange = 'PageFunction(this.value)' value="2">2</button> 
</form> 

Ajax的功能是:

function PageFunction(page){ 
var ajaxRequest; 

if (page==""){ 
    document.getElementById("Pagination").innerHTML = ""; 
    return; 
}else{ 

    if (window.XMLHttpRequest) { 
    ajaxequest = new XMLHttpRequest(); 
    }else{ 
    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
} 
} 

// Create a function that will receive data sent from the server 
ajaxRequest.onreadystatechange = function(){ 
    if(ajaxRequest.readyState == 4 && ajaxequest.status == 200){ 

     var ajaxDisplay = document.getElementById('Pagination'); 
     ajaxDisplay.innerHTML = ajaxRequest.responseText; 
    } 
} 

ajaxRequest.open("GET", "pathjobs-ajax2.php" + page, true); 
ajaxRequest.send(null); 
} 

感謝您的幫助!

+0

如果你使用jQuery的,你可以使用的東西像這樣'var x = $('。mybutton')。attr('value');' - js有這個:'getAttribute(「value」);' –

+0

mate,試試我的解決方案在下面! –

回答

1

隊友,按鈕沒有平變化,試試這個:

<button class = 'pages' type='button' onclick = 'PageFunction(this.value)' value="2">2</button> 
+0

我將它們改爲「onclick」並沒有運氣。 – LearnAWK

0

沒有使用jQuery

var PageFunction = function(val) { 
    console.log(val); 
    // ajax call here. 
} 

<form method="get" onchange = PageFunction(this.value)> 
    <button class = 'pages' type='button' onclick = 'PageFunction(this.value)' value="1">1</button> 
    <button class = 'pages' type='button' onclick = 'PageFunction(this.value)' value="2">2</button> 
</form> 

使用jQuery

$('.pages').on('click',function(e){ 
    console.log($(this).val()); 
    // ajax call here. 
}); 
+0

如果我使用非jQuery選項,我應該在結尾'}'之前加上ajax代碼嗎?對於天真的問題抱歉,我對jQuery沒有任何經驗。 – LearnAWK

+0

對於jQuery選項,下列代碼是否有意義? $('。pages')。on('click',function(e){ console.log($(this).val()); var ajaxRequest; if(val ==「」){ 的document.getElementById( 「分頁」)的innerHTML = 「」; 回報; }其他{ 如果(window.XMLHttpRequest){ ajaxequest =新的XMLHttpRequest();} 其他{ ajaxRequest =新的ActiveXObject(「微軟.XMLHTTP「); }} ajaxRequest.onreadystatechange =函數(){ 如果(ajaxRequest.readyState == 4 && ajaxequest.status == 200){ //碼過長,部分刪除 }); – LearnAWK

+0

我會敦促你先閱讀javaScript的基礎知識,這將幫助你很多https://developer.mozilla.org/en-US/Learn/Getting_started_with_the_web/JavaScript_basics – atinder