开发者

jquery - using a function id in a different function

开发者 https://www.devze.com 2023-02-01 07:06 出处:网络
I have a function like that: function loadcategory(id) { jQuery(\"#categoryArea\").load(\"ajax_post_c开发者_运维知识库ategory.php?catid=\"+id+\"\");

I have a function like that:

function loadcategory(id) {
   jQuery("#categoryArea").load("ajax_post_c开发者_运维知识库ategory.php?catid="+id+"");
  }

im getting id via link like that:

<a href='javascript:loadcategory(".$row['catid'].");'>

i would like to use function loadcategory(id)'s id in a different function. for example:

function different() {
       jQuery("#different").load("ajax_post_category.php?catid="+loadcategory(id)+"");
      }

as you can see i wanted to use +loadcategory(id)+, however i havent gotten any values. well, how can i get that value by jquery? i dont know, can anyone tell me the true way?

regards


The snippet you give where it works:

<a href='javascript:loadcategory(".$row['catid'].");'>

...appears to be some PHP code that generates an href for an anchor tag. Specifically, it appears to output a specific ID so that the generated markup that goes to the browser looks like this:

<a href='javascript:loadcategory("foo");'>

Your second example:

jQuery("#different").load("ajax_post_category.php?catid="+loadcategory(id)+"");

...is using completely client-side code, and an undefined value (id).

To reuse the function, you'll have to either use PHP to output a different ID, or pass the ID into different, or otherwise ensure that some valid ID is passed into loadcategory. Without more information, it's hard to tell what to recommend, but for example, suppose you had a series of anchor tags, and each anchor tag had a data-cat attribute that tol dus what category to load:

<a href='#' data-cat="foo">
<a href='#' data-cat="bar">
<a href='#' data-cat="baz">

Then you could hook up a handler to them that called loadcategory using those values:

jQuery(function($) {
    $('a[data-cat]').click(function() {
        loadcategory($(this).attr('data-cat'));
        return false;
    });
 });

That works by finding all anchor tags that have a data-cat attribute, hooking their click event, and when it occurs getting the data-cat attribute and calling loadcategory with that value.

That's a guess at what you want to do, but hopefully of some use.

0

精彩评论

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

关注公众号