【问题标题】:passing parameters to a jquery javascript function?将参数传递给 jquery javascript 函数?
【发布时间】:2011-04-16 06:10:12
【问题描述】:

我有这个 jquery 函数,我想将 topic 参数传递给,我只是不知道如何传递它,哈哈 :))。

jquery 函数:

function loadPage(url)  //the function that loads pages via AJAX
{
    url=url.replace('#page','');    //strip the #page part of the hash and leave only the page number

    $('#loading').css('visibility','visible');  //show the rotating gif animation

    $.ajax({    //create an ajax request to load_page.php
        type: "POST",
        url: "load_page.php",
        data: 'page='+url,  //with the page number as a parameter
        dataType: "php",    //expect html to be returned
        success: function(msg){

            if(parseInt(msg)!=0)    //if no errors
            {
                $('#change-container').html(msg);   //load the returned html into pageContet
                $('#loading').css('visibility','hidden');   //and hide the rotating gif
            }
        }

    });

}

这是网址:

http://localhost/final/home.php#page2&topic=jquery

当我点击这个链接时,页面加载罚款(使用 jquery),但它没有传递主题参数!

<h3 class="timeline"><?php echo $_GET["topic"]; ?> echo</h3>

所以这个 wnt 回显,因为它 cnt 访问主题参数!如果你们知道我的意思:))

【问题讨论】:

    标签: php javascript jquery url get


    【解决方案1】:

    这个应该可以的

    loadPage('http://localhost/final/home.php#page2?topic=jquery');
    

    如果不是,您的代码可能有问题。


    编辑

    好的,我认为这与您传递给它的 URL 有关,试试这个

    loadPage('#page2&topic=jquery');
    

    希望能帮到你。

    【讨论】:

    • ^^ 好的,有编辑,我认为应该可以完成工作...因为您传递给它的 url 不需要 localhost 等前缀
    • 不,你误会我的意思了@wolfy,很抱歉我改写了这个问题,我不想将链接传递给函数,基本上当我点击链接时,它会把我带到第 2 页,但是当我做一个 get 声明它不是阅读主题时,如果你知道我的意思,我会把代码放在他们的抱歉
    • 我发布了一个新的遮阳篷,因为它是一个完全不同的问题,我认为编辑这个是错误的做法。
    【解决方案2】:

    您在那里使用 $_GET,但 ajax 正在使用 post。

    当你改用它时会发生什么:

    <h3 class="timeline"><?php echo $_POST["topic"]; ?> echo</h3>
    

    【讨论】:

    • 取出'dataType: "php",'会发生什么?
    • 看看这里以备将来参考,dataType 中唯一允许的参数是 xml、html、script 和 json。不是php。 docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests
    • 非常感谢它的工作,我从来没有想过,干杯
    • 没问题,我也花了一段时间才发现。
    猜你喜欢
    • 2014-10-18
    • 2013-01-27
    • 2010-09-27
    • 1970-01-01
    • 2011-10-02
    • 2016-01-16
    • 2017-02-05
    • 1970-01-01
    相关资源
    最近更新 更多