函数中JavaScript函数的正确语法

时间:2012-07-15 16:27:05

标签: php javascript mysql html ajax

函数pagination($total, $limit, $page)返回页码[pg#01 pg#02 pg#03 pg#04 NEXT]。

通过单击页码(例如pg#02),javascript:showPagination('what_here')应该向包含函数pagination()中返回的页码的服务器发送AJAX请求。

两个功能都能正常工作。当我使用showPagination('2')测试JavaScript时,服务器获取此数字(2)。

向服务器发送所单击的请求页码的正确语法是什么?

<td><a href="javascript:showPagination('')"><?php echo pagination($total, $limit, $page); ?></a></td>

3 个答案:

答案 0 :(得分:0)

我倾向于尝试这个:

<td><a href="javascript:showPagination('<?php echo pagination($total, $limit, $page); ?>')"><?php echo pagination($total, $limit, $page); ?></a></td>

但同时使用它(使用相同的语法)可能更方便

onclick='showPagination(<?php echo pagination($total, $limit, $page); ?>);'

答案 1 :(得分:0)

编辑:正如你评论的那样,我想我误解了你的问题。 你可以试试:

<td><a href="javascript:showPagination('<?php echo pagination($total, $limit, $page); ?>');"><?php echo pagination($total, $limit, $page); ?></a></td>

或:

<td><a href="#" onclick="showPagination('<?php echo pagination($total, $limit, $page); ?>');"><?php echo pagination($total, $limit, $page); ?></a></td>

答案 2 :(得分:0)

阅读"This question is actually about the correct syntax of how to send to the server a result from pagination() to showPagination() through Ajax."后,我建议你:

Javascript:

// save current page to global variable
window.globalPageId = 0;

//the pseudo function 
function showPagination(id) {
  id || ( id = globalPageId );
  var xhr = new xmlHttpRequest();
  xhr.open("GET", "/yourPhpPage.php?pageId="+id, true);
  xhr.send();
  xhr.onreadystatechange = function() {
   if(xhr.readyState == 4 && xhr.status == 200) {
    globalPageId = xhr.responseText; //sure if the result is only id of page;
   }
  }
}

然后是html:

<td><a href="javascript:showPagination(this.getAttribute('page-id')" page-id="id of page here, from php function">link text here</a></td>

或:

<td><a href="javascript:showPagination()">link text here</a></td>
相关问题