jquery load part of page, select by variable ID

2019-08-05 09:58发布

I am trying to load a portion of a page using jquery .load(), the the problem is, the div that I am trying to select is a variable

$('#additemsubmit').click(function(event){
    event.preventDefault();
    var location = $(this).parent();
    var reload = '#' + $(this).parent().attr('id');
    $(location).load("/index.php reload");
});

It seems I cant use variables for "location" and "reload"

标签: jquery load
2条回答
放我归山
2楼-- · 2019-08-05 10:52

You need to use string concatenation to pass a selector to .load():

$(this).parent().load("/index.php#" + $(this).parent().attr('id'));
查看更多
爷的心禁止访问
3楼-- · 2019-08-05 10:58

I think this is what you're looking for.

 var reloadId = '#' + $(this).parent().attr('id');
 $.get("/index.php", function(response) {
     $(location).html($(response).find(reloadId));
 });
查看更多
登录 后发表回答