jquery load part of page, select by variable ID

2019-08-05 10:30发布

问题:

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"

回答1:

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));
 });


回答2:

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

$(this).parent().load("/index.php#" + $(this).parent().attr('id'));


标签: jquery load