Passing a variable into a jquery load() function syntax

浪子不回头ぞ 提交于 2020-01-15 09:14:08

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!