如何在HTML中使用jQuery.get()方法检索列表

How to retrieve a list using jQuery.get() method in HTML

本文关键字:get 方法 列表 检索 jQuery HTML      更新时间:2023-09-26

所以我有了这个页面,基本上只需点击一个按钮,我想从另一个HTML文件(或ASP或PHP,无论什么)中检索一个列表,并将其添加到我已经拥有的列表中。我只是不知道该怎么做。有什么想法吗?

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>autocomplete demo</title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
    <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
</head>
<body>
    <script>
        $(document).ready(function() {
            var tags = [ "String1", "String2", "String3" ];
            $("button").click(function() {
                $.get("test.html", function(data, status) {
                   <!-- This is where I want to get the list -->
                   <!-- something like: tags.add(list from test.html) -->
                    alert("Data: " + data + "'nStatus: " + status);
                });
            });
        });
    </script>
    <button>Click me</button>
    <div id="result">hello world</div>
</body>
</html>

此外,如果有人能提供一个简单的HTML页面,为我创建一个伪字符串列表来测试它吗?谢谢

好吧,我也这么认为。

你可以这样做:

$.get("test.html", function (data, status) {
    $(data).filter("#id_of_ul_for_example").find("li").each(function () {
        var val = $(this).text();
        tags.push(val);
    });
});

在这个fiddle中,由于jsFiddle ajax请求,我不得不使用post而不是get。。。http://jsfiddle.net/vxm1ugee/2/

希望能有所帮助。

如果你真的得到了一个值列表,你必须首先使用将列表转换为数组

tags2 = "String4,String5,String6".split(",");

然后javascript的concat方法

result = tags.concat(tags2);