【问题标题】:How to make a link send parameter to the jquery function which then calls ajax function to load?如何使链接发送参数到 jquery 函数,然后调用 ajax 函数加载?
【发布时间】:2026-02-22 20:25:01
【问题描述】:

我整天都在寻找这个问题,并尽我所能尝试了所有其他变体,但我无法使以下代码工作。如何使链接向 jquery 函数发送参数,然后调用 ajax 函数从 REST API 加载数据?我不想链接打开新页面,但想在同一页面上加载数据。 e.preventDefault 根本不起作用。所以,看不到任何 ajax 活动。

 $(document).ready(function(){

   $("$decklist").bind("click", function(event) {

    var url = $(this).attr("href");
    alert("loading via proxy: " + url);
      $.ajax({
        type: "GET",
        url: "http://localhost:8080/flashcardapp/webservices/flashcardapp/",
        data: "url="+url, 
        success: function(data){
          alert("finally got data " + data);
        }
      });
    event.preventDefault();
 });

});

【问题讨论】:

  • 您在 jquery 选择器中使用$..我认为这是错误的。
  • 你问的太混乱了。您正在尝试发出 ajax 请求。除此之外,您还想做什么?

标签: jquery ajax


【解决方案1】:

这可能只是一个错字。在你的 jQuery 选择器(decklist)中尝试# 而不是$。示例:

$(document).ready(function(){

    // HERE. Notice how it says #decklist below and not $decklist
    $("#decklist").bind("click", function(event) {
        event.preventDefault();
        // ... the rest of your code
    });
});

此外,我们这里没有您的 HTML,但我认为您的链接看起来像这样(带有id 属性)。确保id="decklist" 而不是id="$decklist"

<a href="#" id="decklist">Something</a>

【讨论】: