php - 使用<a>标签在ajax中传递多个值,并检索提交的值

标签 php javascript ajax

如果要使用输入标记,这是一个非常简单的函数,不幸的是我必须使用链接标记,因为我正在使用字母分组来检索不同页面上的值。

这是我找到的链接标记的代码,用于保存要提交给ajax的值

echo '<a onclick="passPagination(\'3\');passLetter(\'S\')">NEXT</a>';

我想从另一页调用所有字母“s”页“3”。

        function passLetter(str)
    {
    if (str=="")
      {
      document.getElementById("retail_group").innerHTML="";
      return;
      } 
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("retail_group").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","otherpage.php?letter="+str,true);
    xmlhttp.send();
    }

    function passPagination(str)
    {
    if (str=="")
      {
      document.getElementById("retail_group").innerHTML="";
      return;
      } 
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
                    document.getElementById("retail_group").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","otherpage.php?pageno="+str,true);
    xmlhttp.send();
    }

我还想再次调用传递到其他页面的信件,以便我将其作为我的分页链接的引用,该链接也保存在此页面中。 回显“下一个”;

我真的很困惑,因为我不熟悉链接标签和ajax一起工作,而且你可能已经注意到,从我很长的ajax代码来看,我对ajax也不太擅长。

非常感谢您的帮助,谢谢。

我对 ajax 真的很陌生。 :)

最佳答案

解决方案是创建一个具有多个参数的函数:

function passPaginationAndLetter(page, letter)
{
if (page=="" || letter == "")
  {
  document.getElementById("retail_group").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
                document.getElementById("retail_group").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","otherpage.php?letter="+letter+"&pageno="+page,true);
xmlhttp.send();
}

您现在可以使用

调用它

echo '<a onclick="passPaginationAndLetter(\'3\',\'S\')">NEXT</a>';

区别在于您没有调用两个 AJAX 方法,而只是调用一个方法。

关于php - 使用<a>标签在ajax中传递多个值,并检索提交的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9319397/

相关文章:

php - 将行添加到 html 表格

PHP - 可以将成员函数分配给变量吗?

jQuery JSONP "Uncaught SyntaxError: Unexpected token :"

javascript - 加载多个 jQuery 版本

c# - 在内容页 ASP.NET 中使用时,Javascript 不返回正确的值

jQuery 鼠标悬停调用单击

php - 将所有内容传递到一个位置 nginx

php - 连接到 MySQL 时出现 PDO 错误

javascript - 选择可以添加新记录的框

javascript - Angular2 ng用于从索引中添加数字++