0

「typeId」および「idValue」属性を持つ MVC ビューに HTML SPAN (クラス: displayText) があります。目的は、渡された「typeId」と「idValue」のデータベース テーブルから DisplayText プロパティを取得することです。

ビューにそのような 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 は MVC ビューの HiddenField の ID であり、データベースから表示値を取得する次の MVC コントローラー アクションの URL が含まれています。

[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 コントロールの値を更新しません。Controller アクションが呼び出されていることを Firebug で監視できます。また、正しい Json 応答が返されますが、スパンは更新されません。考えられる理由は何ですか?そして、それを修正する方法は?

4

2 に答える 2

1

dataTypeajax レスポンスの を指定してみてください。また、innerTextfirefox では動作しません。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);
        }
    });
于 2013-07-31T05:32:48.573 に答える
1

の代わりにText()またはを試してください。html()innerText()

このような、

$('span.displayText')[index].Text(result);
于 2013-07-31T05:25:22.760 に答える