javascript - 单击按钮时填充 javascript 变量

标签 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 ajax请求;//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" with value="nextpage") 被按下?当按下时,JavaScript 变量 nextpage 应包含“nextpage”

最佳答案

发布变量而不是使用 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;
    }
  }
}

关于javascript - 单击按钮时填充 javascript 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36992770/

相关文章:

javascript - 如何使用模板选项将 JSON 对象数据绑定(bind)到 knockout 绑定(bind)?

javascript - 用 next 和 prev 突出显示 P 标签

javascript - 获取浏览器宽度并根据大小隐藏元素?

php - 典型的 PHP 错误

jquery - Ajax 加载后触发 WordPress 插件/短代码

javascript - 在基于 require.js 的应用程序的 r.js 构建中手动设置区域设置不起作用

php - Laravel 中如何创建全局变量

PHP 使用 REST 插入 MySQL 数据库

javascript - 使用 Jquery 搜索 - 适用于多个术语

jquery - 使用 SCSS 的关键帧动画不起作用