簡體   English   中英

使用在js函數上接收到的整數作為參數來獲取與傳遞的整數作為參數具有相同ID的div ID

[英]using integer received on js function as param to fetch div id with same id as passed integer as param

我在js函數上收到一些整數作為參數,我想使用也代表div id的值將結果附加到div中。

function DisplaySomeData(someData, code) {
        $.ajax({
            url: formatUrl('...'),
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify({ data: someData}),
            success: function (result) {
                $("#+code").html(result);// how to use code to recognize div id?
            },
            error: ...
        });

您沒有正確連接選擇器字符串。 嘗試這個:

$('#' + code).html(result)

您的代碼中存在一些語法錯誤。 如果要選擇在DisplaySomeData函數中傳遞其id屬性'code'的div。 下面是修改后的語法。

function DisplaySomeData(someData, code) {
    $.ajax({
        url: formatUrl('...'),
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({ data: someData}),
        success: function (result) {
            $("#"+code).html(result);// or use $("div[id='"+code+"']").html(result);
        },//Both the syntax should work
        error: ...
    });

注意:如果您覺得這很有用。 請標記為已回答。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM