2013-07-31 40 views
0

得到更新我有一個HTML SPAN(類:displayText),其中有「TYPEID」我的MVC視圖和「idValue」屬性。 目的是從傳遞「TYPEID」和「idValue」數據庫表獲取DisplayText屬性。JSON響應不能在Firefox

我在我的視圖中有這樣的SPAN編號,所以我在document.ready()函數中調用以下方法來獲取顯示文本並在視圖中顯示它,以便用戶不需要等待。

$('span.displayText').each(
function (index) { 
    var url = $('#UrlGetDisplayName').val(); 
    $.ajax({ 
     type: "GET", 
     url: url, 
     data: ({ typeId: $(this).attr('typeId'), 
      value: $(this).attr('idValue') 
     }), 
     success: function (result) { 
      $('span.displayText')[index].innerText = result; 
     } 
    }); 
}) 

在上面的代碼中,UrlGetDisplayName是HiddenField的對包含以下MVC的控制器動作,其負責從數據庫帶來顯示值的URL我的MVC圖的ID。

[HttpGet] 
public virtual JsonResult GetDisplayTextByType(string typeId, string idValue) 
{ 
    string displayText = ""; 
    /* Code to call a database method to fetch text by type, and assign it to displayText */ 
return Json(displayText, JsonRequestBehavior.AllowGet); 
} 

這在IE和Chrome中完美工作,但在Firefox中它不會更新SPAN控件中的值。我可以在Firebug監視控制器動作被調用,也,它返回正確的JSON響應,但不更新跨度。可能的原因是什麼?以及如何解決它?

+0

.innerText將無法工作在Firefox瀏覽器。 http://stackoverflow.com/questions/1359469/innertext-works-in-ie-but-not-in-firefox – anil

回答

1

請嘗試Text()html(),而不是innerText()

這樣,

$('span.displayText')[index].Text(result); 
+0

很酷,我意識到這是在Firefox中不支持的innerText屬性的問題。我發現了一個在http://stackoverflow.com/questions/1359469/innertext-works-in-ie-but-not-in-firefox另一種解決方案。檢查bobnice的答案。感謝您的幫助,這有助於我確定問題所在的區域,因此將其標記爲答案。 :) – Nirman

1

可以嘗試使用AJAX response.Also的dataTypeinnerText不會firefox.So工作,嘗試使用html()text()

$.ajax({ 
     type: "GET", 
     url: url, 
     dataType:'json', 
     data: ({ typeId: $(this).attr('typeId'), 
      value: $(this).attr('idValue') 
     }), 
     success: function (result) { 
      var response = JSON.stringify(result); 
      response  = JSON.parse(response); 
      console.log(response);//Logging the response to browser console 
      $('span.displayText')[index].html(response); 
     } 
    });