问题
I am trying to find out how to pass that a variable string staffID into an id selector in the load() function. Here is the piece of code:
$('li.staffAsset').click(function () {
var staffID = $(this).attr("id");
openDialog('#descrDialog');
$('#staffDescr p').load('/staffDescr.html $("#" + staffID)');
});
This doesn't work. Basically there are divs with id="staffID" inside the staffDescr.html. I just can't seem to find the proper syntax to pass that variable string as a proper id into the load() function. Can anyone help please?
回答1:
You don't pass the dollar function into the string. Just use this:
$('#staffDescr p').load('/staffDescr.html #' + staffID);
See the documentation: Loading Page Fragments.
Also, there's no need for $(this).attr("id")
. Just use this.id
:
$('li.staffAsset').click(function () {
openDialog('#descrDialog');
$('#staffDescr p').load('/staffDescr.html #' + this.id);
});
回答2:
You messed the quotes. To simply put the id in your string you might replace
$('#staffDescr p').load('/staffDescr.html $("#" + staffID)');
with
$('#staffDescr p').load('/staffDescr.html $("#"' + staffID+')');
But you probably want
$('#staffDescr p').load('/staffDescr.html #' + staffID);
if you're trying to load a page fragment.
来源:https://stackoverflow.com/questions/13567992/passing-a-variable-into-a-jquery-load-function-syntax