单击按钮时填充javascript变量

时间:2016-05-02 22:42:21

标签: javascript php jquery variables onclick

为了与api结合构建分页,当我按 next 按钮时,我希望将值next分配给javascript变量,以便将其发送到PHP脚本。到目前为止我有这个,但我对Javascript不是很熟悉:

nextpage = document.getElementById('nextpage').onclick = document.getElementById('nextpage').value;

if(!nextpage==0){
    link =link+"&nextpage="+nextpage;
}

这样Javascript变量就会被发布到url中,所以另一方面PHP脚本可以像这样选择它。对于我使用的其他变量:

mantinee = document.zoekform.mantinee.value;

if(!mantinee==0){
    link =link+"&mantinee="+mantinee;
}

如果我这样做,那么它会直接发布到url,所以PHP脚本总是认为它需要跳到下一页,这不是我的意图。单击按钮会在单击时调用ajaxgetinfo()

query.php

if(isset($_POST['nextpage']))
{
  $nextpage = $_POST['nextpage'];
}

以下是获取每个变量并传递它们的所有javascript。这是下一页也需要运行的地方

function vorige(){
sort = 'vorige';
ajaxGetInfo(sort);
}

函数ajaxGetInfo(排序) {     var ajaxRequest; // De变量wat Ajax mogelijk maakt

try{
    // Chrome, Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
} catch (e){
    // Internet Explorer Browsers
    try{
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
}

link="query.php?";

datumreeks='';
if(document.zoekform.zo.checked==true){datumreeks="zondag";}
if(document.zoekform.ma.checked==true){datumreeks=datumreeks+"maandag-";}
if(document.zoekform.di.checked==true){datumreeks=datumreeks+"dinsdag-";}
if(document.zoekform.wo.checked==true){datumreeks=datumreeks+"woensdag-";}
if(document.zoekform.don.checked==true){datumreeks=datumreeks+"donderdag-";}
if(document.zoekform.vr.checked==true){datumreeks=datumreeks+"vrijdag-";}
if(document.zoekform.za.checked==true){datumreeks=datumreeks+"zaterdag-";}
link = link+"&datumreeks="+datumreeks;

datepicker = document.zoekform.datepicker.value;
if(!datepicker==0){link =link+"&datepicker="+datepicker;}

datepicker2 = document.zoekform.datepicker2.value;
if(!datepicker2==0){link =link+"&datepicker2="+datepicker2;}

zaal = document.zoekform.zaal.value;
if(!zaal==0){link =link+"&zaal="+zaal;}

genre = document.zoekform.genre.value;
if(!genre==0){link =link+"&genre="+genre;}

profiel = document.zoekform.profiel.value;
if(!profiel==0){link =link+"&profiel="+profiel;}

internationaal = document.zoekform.internationaal.value;
if(!internationaal==0){link =link+"&internationaal="+internationaal;}

prijslaag = document.zoekform.prijslaag.value;
if(!prijslaag==0){link =link+"&prijslaag="+prijslaag;}

prijshoog = document.zoekform.prijshoog.value;
if(!prijshoog==0){link =link+"&prijshoog="+prijshoog;}

mantinee = document.zoekform.mantinee.value;
if(!mantinee==0){link =link+"&mantinee="+mantinee;}

document.getElementById('nextpage').onclick = function(e){
  ajaxRequest.open("POST", link, true);
  if (nextpage) ajaxRequest.send("nextpage=yes");
}

ajaxRequest.open("GET", link, true);
ajaxRequest.send(null);
document.getElementById("info").innerHTML = '<div align=center><i class="material-icons w3-spin w3-jumbo">refresh</i></br>Blijft dit staan? <a href="" onclick="ajaxGetInfo()">Klik hier.</a></div>';


ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
        document.getElementById("info").innerHTML = ajaxRequest.responseText;
    }
  }
}

我怎样才能使Javascript变量(nextpage)保持为空,直到按下按钮(name="nextpage"并带有value="nextpage")?按下时,javascript变量nextpage应包含“nextpage”

2 个答案:

答案 0 :(得分:2)

发布您的变量而不是使用GET:

document.getElementById('nextpage').onclick = function(e){

  ajaxRequest.open("POST", link, true);
  if (nextpage) ajaxRequest.send("nextpage=yes");
  document.getElementById("info").innerHTML = '<div align=center><i class="material-icons w3-spin w3-jumbo">refresh</i></br>Blijft dit staan? <a href="" onclick="ajaxGetInfo()">Klik hier.</a></div>';


  ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
      document.getElementById("info").innerHTML = ajaxRequest.responseText;
    }
  }
}

答案 1 :(得分:0)

如果我的问题正确,你的问题是nextpage = [...]

以这种方式尝试:

document.getElementById('nextpage').onclick = function(e){
    //do your request etc
}

这是一个例子:https://jsfiddle.net/nmLeetgu/

相关问题