[英]How to check for contents of a loaded div tag using jquery load?
I'm working with jqueries address change event and am hitting a roadblock when a user copies and pastes a URL in the browser. 我正在处理jqueries地址更改事件,当用户在浏览器中复制并粘贴URL时遇到障碍。 I need to fist load a portion of the page that contains a form. 我需要首先加载包含表单的页面的一部分。 I could do this after every pagination call but it seems really ineffecient. 我可以在每次分页调用之后执行此操作,但是这似乎效率很低。
Here is my current code block: 这是我当前的代码块:
$.address.change(function(e) {
var urlAux = e.value.split('=');
var page = urlAux[0];
var start = urlAux[1];
if (page == "/visits") {
$.address.title("Profile Views");
if (start) {
$('#start').val(start);
// ***** If a user has copied and pasted this URL with a start value then I first need to load visits.php in the main div tag. Is it possible to see if this is loaded or not?
$.post("visits_results.php", $("#profile_form_id").serialize(),
function(data) {
$('#search_results').html(data);
location.href = "#visits=" + start;
});
}
else {
var args = localStorage.getItem("visits");
$('#main').load("visits.php?" + args, function () { });
}
}
My attempted work around was this: 我尝试解决的问题是这样的:
var args = localStorage.getItem("visits");
$('#main').load("visits.php?" + args, function () {
$('#start').val(start);
$.post("visits_results.php", $("#profile_form_id").serialize(),
function(data) {
$('#search_results').html(data);
location.href = "#visits=" + start;
});
});
There must be a better way...this is realoading the same portion of the page (visits.php) with every pagination event. 必须有更好的方法...这是在每次分页事件中重新加载页面的相同部分(visits.php)。 Is there a better way to load URLs and not have them trigger an address change? 有没有更好的方法来加载URL而不让它们触发地址更改?
Using paul's work around from his comments, but instead of Regex'ing html content in the visits.php form this solution will look for data() attached to #mainID. 使用paul从他的评论中解决的方法,而不是Regex在visits.php表单中显示html内容,此解决方案将查找附加到#mainID的data()。
Paul's work around notes: 保罗的工作围绕着笔记:
After a bit more hacking I came up with this solution that seems to do the trick. 经过更多的黑客攻击后,我想出了这个解决方案,似乎可以解决问题。 I'm not sure how good it is but it seems to do the trick. 我不确定它有多好,但似乎可以解决问题。 I now get the main div id and do a regex match on a unique string in the form. 现在,我得到主要的div ID并在表单中的唯一字符串上进行正则表达式匹配。 If I don't see it I load the form and then load the results. 如果看不到,请加载表格,然后加载结果。 Not sure if this is good practice or not but it seems to solve my issue. 不知道这是否是个好习惯,但似乎可以解决我的问题。
Methodology to use .data() instead of a regex search of visits.php's html: 使用.data()而不是visits.php的html的正则表达式搜索的方法:
/*check if we're missing visits.php by looking for data() flag*/
if( !($("#main").data()["hasVisitsPhp"]) ){
var args = localStorage.getItem("visits");
$('#main').load("visits.php?" + args, function () {
$('#start').val(start);
$.post("visits_results.php", $("#profile_form_id").serialize(),
function(data) {
/* we've loaded visits.php, set the data flag on #main*/
$('#main').data("hasVisitsPhp","loaded");
$('#search_results').html(data);
location.href = "#visits=" + start;
});
});
}
try window.location.hash
instead. 请尝试使用window.location.hash
。 Changing the whole href can/will trigger a whole-page reload, while changing just the hash by itself should at most cause the page to scroll. 更改整个href可以/将触发整个页面的重新加载,而仅更改哈希本身最多应该导致页面滚动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.