简体   繁体   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

I'm receiving some integer on js function as parameter and I want to use that value which also represents div id to append result inside div. 我在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: ...
        });

You aren't concatenating the selector string correctly. 您没有正确连接选择器字符串。 Try this: 尝试这个:

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

Some Syntax error in your code. 您的代码中存在一些语法错误。 If you want to select div whose id attribute 'code' which you are passing in DisplaySomeData function. 如果要选择在DisplaySomeData函数中传递其id属性'code'的div。 below is modified syntax. 下面是修改后的语法。

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: ...
    });

Note : If you found this useful. 注意:如果您觉得这很有用。 Please mark it as answered. 请标记为已回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM