我想在调用 $.ajax() 后获取带有 ID 的 div 元素 这是我的代码:
$.ajax({
url: "PO_Header.php",
data: "supplier="+selectedId,
cache: false,
success: function(html){
$("#PO_Header").empty();
$("#PO_Header").append(html);
}
});
$("#PO_Header").append(html);
将追加整个 html 页面,我想要的是获取具有特定 id 的元素。假设在PO_Header.php
中,带有 id='mydiv'
的 div 元素将被注入(inject)到我的当前页面中。
最佳答案
使用jQuery的加载:
$('#PO_Header').load('PO_Header.php #mydiv', { 'supplier': selectedId } );
它允许您加载页面片段。正如他们的documentation指出:
Loading Page Fragments The .load() method, unlike $.get(), allows us to specify a portion of the remote document to be inserted. This is achieved with a special syntax for the url parameter. If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded.
因此它只会注入(inject) <div id="myDiv"></div>
从 PO_Header.php 到您的元素中。
关于jquery - 使用jquery ajax后如何通过选择器获取元素? $.ajax(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9424000/