如何从函数外部获取函数的id值

huangapple go评论58阅读模式
英文:

How to get the id value from a function from outside the function

问题

我需要在代码外部使用id来设置动态创建的输入框的值。但是我无法在代码外部访问id,并且在代码内部无法设置输入框的值。请问我应该在哪一行代码中获取id以便在代码外部使用它?

我尝试在不使用id的情况下在代码外部设置输入框的值,像这样:$('#pIP').val("8"); 这个方法可行。

我尝试在代码内部不使用id的情况下设置输入框的值,像这样:$('#pIP').val("8"); 但这不起作用。

我尝试在代码外部使用id来设置输入框的值,像这样:$('#pIP').val(id); 但这不起作用。

我尝试在代码内部使用id来设置输入框的值,像这样:$('#pIP').val(id); 但这不起作用。

英文:

I have this code that dynamically produces the id of an element.

function get_data_mapping_for_id(path) {

    let id = $(path).attr('data-map-id');

    Swal.fire({
        title: 'Loading data...',
        willOpen: function() {

            $('.site-plan').addClass("zoom-svg");
            $('path[data-map-id="' + id + '"]').addClass("highlight-path")

            Swal.showLoading();
            $.get(endPoint, {
                action: 'get_data_mapping_for_id',
                id
            }, function(data) {
                Swal.hideLoading();
                jsonResp = JSON.parse(data);
                const table = jsonToHTMLTable(jsonResp, $(path));
                Swal.update({
                    title: jsonResp.title,
                    html: table,
                    confirmButtonText: 'OK'
                });
            });
        },
        willClose: function() {
            $('.site-plan').removeClass("zoom-svg");
            $('path[data-map-id="' + id + '"]').removeClass("highlight-path");
        }
    });
}

I need to set the value of a dynamcally created input with the 'id'.

BUT I cannot access the 'id' outside of the code.

AND I cannot set the input value while inside the code.

what line of code do I use to get the 'id' outside the code so I can use it?

I tried setting the input value without using the 'id' outside the code like this: $('#pIP').val("8"); and it works.

I tried setting the input value without using the 'id' inside the code like this: $('#pIP').val("8"); and it does not work.

I tried setting the input value using the 'id' outside the code like this: $('#pIP').val(id); and it does not work.

I tried setting the input value using the 'id' inside the code like this: $('#pIP').val(id); and it does not work.

答案1

得分: 0

你可以“返回”id值,或将其设置为全局变量。

英文:

You can "return" the id value or set it as a global variable.

huangapple
  • 本文由 发表于 2023年6月12日 22:05:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定