如何将按钮值发送到ajax

时间:2015-07-20 04:45:36

标签: php ajax

我尝试将按钮值(整数)作为页码发送到ajax函数然后发送到php文件,但无法使其工作。按钮是:

<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); 
}

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

交配,按钮没有onchange,试试这个:

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

答案 1 :(得分: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.
});