[英]ie and jquery $.get not working
我正在嘗試使用$.get
調用更新div的內容,但是在ie(9).
失敗ie(9).
js
是這個
function UpdateElementOfParent(box_id, page_ref, template_ref)
{
$.get("get_content.php", { box_id: box_id, page_ref: page_ref, template_ref:template_ref } )
.done(function(data) {
$('#'+box_id).html(data);
});
}
而get_content.php
是這個
<?php
include("connect.php");
$page_ref = $_GET['page_ref'];
$template_ref = $_GET['template_ref'];
$box_id = $_GET['box_id'];
$sql = mysql_query("SELECT * FROM site_content WHERE page_ref='$page_ref' AND template_ref='$template_ref' AND box_id='$box_id' AND box_type='text'");
while($row=mysql_fetch_array($sql))
{
echo stripslashes($row['content']);
}
?>
在firefox/chrome/safari and opera.
php更新數據庫,但div ("#"+box_id)
不會在ie
更新(僅手頭有ie9
,所以不知道它是否只是9或其他版本)
有什么線索嗎?
快速更新
似乎是在緩存中保存了先前$ .get調用中的某些數據。 基本上,我在屏幕上有一個div,當用戶單擊一個按鈕時,將打開一個帶有textarea的圖層,該文本可通過nicedit進行編輯。 在textarea中填充$ .get,然后用戶單擊“保存”,隱藏圖層,並使用相同的$ .get調用更新父頁面上的原始div。
在ie中,如果我更改內容,則db被更新,但是div沒有更新,當我打開圖層時,它仍顯示舊數據。
$ .get的第一個電話是這個
$.get("get_content.php", { box_id: box_id, page_ref: page_ref, template_ref:template_ref } )
.done(function(data) {
document.getElementById("edit_content").value=data;
area1 = new nicEditor({fullPanel : true}).panelInstance("edit_content",{hasPanel : true});
});
警報的數據不會在IE中顯示更新的文本,因此肯定與$ .get調用有關
這不是一個答案,因為問題不完整,但是我需要發布代碼注釋來幫助OP。
正如您提到的,PHP可以很好地工作,問題可能是IE不喜歡jQuery中的動態選擇器。 請嘗試以下幾個選項:
1)更改$('#'+box_id).html(data);
至:
var id = '#'+box_id;
$(id).html(data);
2)嘗試記錄或警告元素出去,以查看IE是否正確地獲得了元素:
var elem = $('#'+box_id);
alert(elem);
elem.html(data);
如果該元素存在,它將顯示為[HTMLDivElement]
或類似的內容。
3)如果其他所有方法均失敗,請查看此普通JS是否可在IE中運行,以驗證它不是jQuery選擇器問題。
var elem = document.getElementById(box_id);
alert(elem);
elem.innerHTML = data;
我解決了這個問題。 與選擇器無關,但與參數變量box_id
的范圍有關。
將功能更改為:
function UpdateElementOfParent(box_id, page_ref, template_ref) {
myBox = box_id;
$.get("get_content.php", { box_id: box_id, page_ref: page_ref, template_ref:template_ref })
.done(function(data) {
$('#'+myBox).html(data);
});
}
說明:
AJAX回調函數無權訪問UpdateElementOfParent
的局部變量
好的問題解決了,我知道這很明顯。
在原始$ .get函數調用中,我必須設置document.ready狀態
function get_edit_content(box_id,page_ref,template_ref)
{
$(document).ready(function() { <<<<<HERE
if(area1) {
area1.removeInstance('edit_content');
area1 = null;
document.getElementById("edit_content").value="";
}
$.get("get_content.php", { box_id: box_id, page_ref: page_ref, template_ref:template_ref } )
.done(function(data) {
document.getElementById("edit_content").value=data;
document.getElementById("page_ref").value=page_ref;
document.getElementById("template_ref").value=template_ref;
document.getElementById("box_id").value = box_id;
area1 = new nicEditor({fullPanel : true}).panelInstance("edit_content",{hasPanel : true});
});
});
}
感謝所有的投入
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.