AJAX 响应文本作为 DOM?

发布于 2024-11-19 23:54:23 字数 312 浏览 2 评论 0原文

考虑使用 jQuery 的以下函数:

function getVal() {
    jQuery.get('/relative/url/', function (data) {
        return data.getElementById('myInput').value;
    }
}

这基本上就是我想要做的,但我不知道应该如何完成。 我知道唯一可行的方法涉及框架或innerHTML,但我无法使用它们,因为我必须等待元素准备好。唯一的方法是使用回调,并且此函数必须返回元素的值而不是其他值。 我的逻辑可能有问题,请大家指正。

Consider the following function using jQuery:

function getVal() {
    jQuery.get('/relative/url/', function (data) {
        return data.getElementById('myInput').value;
    }
}

This is basically what I want to do, but I have no idea how it should be done.
The only methods I know would work involve frames or innerHTML which I can't use because I have to wait for the element to be ready. The only way to do that is to use a callback, and this function must return the value of the element rather than something else.
My logic is likely faulty here, so please feel free to correct me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

新人笑 2024-11-26 23:54:23

首先,对于当前的结构,您应该使用回调来返回值。要解析通过 AJAX 检索到的 HTML 字符串,您可以将其交给 jQuery,然后像往常一样查询它。

function getVal(callback) {
    jQuery.get('/relative/url/', function (data) {
        callback($(data).find('#myInput').val());
    }, 'html');
}

然后,当您调用函数 getVal 时,您需要提供回调:

getVal(function(input_val) {
    /**
     * This code will be run once the GET request finishes.
     * It will be passed one parameter - the value of #myInput in the HTML
     * response to the request (see getVal function).
     */

    alert(input_val);
});

First of all, with your current structure you should use a callback to return the value. To parse the HTML string retrieved via AJAX, you can hand it to jQuery and then query it just as usual.

function getVal(callback) {
    jQuery.get('/relative/url/', function (data) {
        callback($(data).find('#myInput').val());
    }, 'html');
}

Then, when you are calling the function getVal, you'll need to provide a callback:

getVal(function(input_val) {
    /**
     * This code will be run once the GET request finishes.
     * It will be passed one parameter - the value of #myInput in the HTML
     * response to the request (see getVal function).
     */

    alert(input_val);
});
挽容 2024-11-26 23:54:23

不,你不能这样做......因为它是异步调用。您需要的是为您的代码提供回调以返回值

function getVal(callback) {
    jQuery.get('/relative/url/', function (data) {
        callback(data.getElementById('myInput').value);
    }
}

getVal(function (value) {
  alert(value);
});

No, you could not do that.. since it is ansync call. What you need is to provide a callback to you code, to return the value

function getVal(callback) {
    jQuery.get('/relative/url/', function (data) {
        callback(data.getElementById('myInput').value);
    }
}

getVal(function (value) {
  alert(value);
});
断念 2024-11-26 23:54:23

如果它是有效的 html 标记,您可以使用选择器浏览其 xml:

*[id=myInput]

或者您可以只在页面中的某个虚拟元素上呈现标记,然后进行查找:

function getVal() {
    jQuery.get('/relative/url/', function (data) {
        dummyNode.innerHTML = data; //not sure data points to responseTxt
        return getElementById('myInput').innerHTML;
    }
}

if the it's valid html markup, you can use browse its xml with the selector:

*[id=myInput]

or you can just render the markup on some dummy element in your page and then do you lookup:

function getVal() {
    jQuery.get('/relative/url/', function (data) {
        dummyNode.innerHTML = data; //not sure data points to responseTxt
        return getElementById('myInput').innerHTML;
    }
}
笑,眼淚并存 2024-11-26 23:54:23

除非将元素添加到 dom 树中,否则您无法执行此操作。

function getVal() {
    jQuery.get('/relative/url/', function (data) {
        return $(document.body).append(data).find('#myInput').val();
    }
}

You cannot do that unless the elements are added to dom tree.

function getVal() {
    jQuery.get('/relative/url/', function (data) {
        return $(document.body).append(data).find('#myInput').val();
    }
}
南薇 2024-11-26 23:54:23

有几个问题。首先,您不能从这样的回调中返回。您只需返回匿名函数本身,而不是从 getVal() 方法返回。

为了解决这个问题,您可以返回jXHR对象并应用一些魔法:

function getVal() {
    return jQuery.get('/relative/url/');
}

getVal().done(function (data) {
    var val = $( data ).find('#myInput').val();
    // do something with val
}

我不知道data的结构是什么样的,但它应该以这种方式工作。如果不是,可能是因为 myInput 位于顶层。在这种情况下,请将 .find() 替换为 .filter()

Several problems there. First, you cannot return from a callback like that. You would just return to the anonymous function itself, not from the getVal() method.

To solve that, you can return the jXHR object and apply some magic:

function getVal() {
    return jQuery.get('/relative/url/');
}

getVal().done(function (data) {
    var val = $( data ).find('#myInput').val();
    // do something with val
}

I don't know how the structure from data looks like, but it should work that way. If not, its probably because of myInput is on the top level. In that case, replace .find() with .filter().

娜些时光,永不杰束 2024-11-26 23:54:23

哦,好吧。我明白了。我认为我没有提供足够的信息。我认为上下文无关紧要。好的,这是一个描述我的解决方案的示例。

function getVal() {
    $.get('/relative/url/', function (data) {
        $.get('relative/url/2', function (data2) {
            var data_obj = [];
            data_obj.push({
                "data_1":data[i].name
            }, {
                "data_2":$(data).find('#myInput').val()
            });
        });
    }
}

Oh, alright. I've got it. I don't think I provided enough information. I assumed context was irrelevant. Alright, here's an example depicting my solution.

function getVal() {
    $.get('/relative/url/', function (data) {
        $.get('relative/url/2', function (data2) {
            var data_obj = [];
            data_obj.push({
                "data_1":data[i].name
            }, {
                "data_2":$(data).find('#myInput').val()
            });
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文