簡體   English   中英

函數中JavaScript函數的正確語法

[英]The correct syntax for a JavaScript function within a function

pagination($total, $limit, $page)功能pagination($total, $limit, $page)返回頁碼[pg#01 pg#02 pg#03 pg#04 NEXT]。

通過單擊頁碼(例如pg#02), javascript:showPagination('what_here')應該向服務器發送AJAX請求,其中包含從函數pagination()返回的pagination()

兩種功能均正常工作。 當我使用showPagination('2')測試JavaScript時,服務器將獲得此數字(2)。

向服務器發送被單擊的請求頁碼的正確語法是什么?

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

我傾向於嘗試這樣做:

<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); ?>);'

編輯:正如您所說,我想我誤解了您的問題。 您可以嘗試:

<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>

閱讀"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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM