开发者

passing #foo selectors into $.get() data URL parameters

开发者 https://www.devze.com 2023-03-30 17:50 出处:网络
I am trying to make an ajax call using $.get() where I want to pass in a specific element selector (#id) into the data:

I am trying to make an ajax call using $.get() where I want to pass in a specific element selector (#id) into the data:

$.get('http://www.url.com',
    {
     param1: 'param1Val',
     param2: 'param2Val',
     #id:    'selector'
     }
     function(data) {
        ...
     });

I could try passing it in as

$.get('http://www.url.com#id', ... );

EDIT The purpose of attempting to use a #selector within the request URL 开发者_开发技巧is to grab only a specific "chunk" of HTML from within a specified element -- hence, I use remote_URL#element_id in the address. This works the same as any <a href="#go_to_element">Go to element with id="go_to_element"</a> link which will send you to a specific part of the page.

See the answer below.


If you're trying to pass "#id" as a query string paramter then you'll have to wrap it in quotes. Also, you're missing a comma after your second parameter. The function call you're looking for should be written as:

$.get('/mypage.php',   // local page
    {
        param1: 'param1Val',
        param2: 'param2Val',
        '#id':  'selector'
    },
    function(data) {
        // do something here...
    }
);


jQuery's $.load() method allows you to send an AJAX request to a specific section of another page, returning the HTML contents of that section in the data parameter in the $.load method's callback function.

http://api.jquery.com/load/ (Re: Load Page Fragments)

$.load('www.localurl.com#container',
    function(data) {
        // do something
    });

Thanks to those who attempted to help on this! Sorry the question was poorly phrased.

Cheers

0

精彩评论

暂无评论...
验证码 换一张
取 消