【问题标题】:Parse XML in jQuery by passing an id通过传递 id 在 jQuery 中解析 XML
【发布时间】:2011-10-23 14:57:41
【问题描述】:

我有 4 个 xml 块,我想用 jQuery 进行一次 ajax 调用来解析它们。现在四个xml块彼此不同(但结构相同)。我试图通过向每个 xml 块传递一个 id 来解析这个 xml,但是在我的 ajax 调用中,我无法检索 id 属性,因为它是我需要捕获以传递给 ajax 函数的第一件事。

我尝试过这样的事情:

XML 在这里:

       <dropdown>
            <test id="1">
            <optionheading>
                <heads>Heading 1</heads>
                            <value>
                                    <values images='images/1.gif'>Option1</values>
                                    <values images='images/2.gif'>Option2</values>
                                    <values images='images/3.gif'>Option3</values>
                            </value>
            </optionheading>
    .....................................

    ............................
    </test>
    <test id='2">
    <optionheading>
                <heads id="b">H..................
    ..................
    ............
    </test>
</dropdown>

我需要获取 test1、test2、et-al 的 id

Ajax 来了:

$("document").ready(function() {
                $.ajax({ 
                    type: "GET",
                    url:"dropdown.xml",
                    dataType: "xml",
                    success :function(xml) {
                        var select = $("#mySelect"); 
                        //I am getting id here
                        var id=$(xml).find('test').attr('id'); 

                        $(xml).find('test'+id).each(function() {
                            $(this).find('optionheading').each(function() {
                            var opthead = $(this).fin......................

【问题讨论】:

    标签: jquery xml ajax xml-parsing


    【解决方案1】:

    如果我理解你的话,应该会发生以下情况(输出粗体):

    • &lt;test id="IDHERE"&gt; 应该填充 &lt;select ID="IDHERE"&gt;
    • &lt;optionheading&gt; 应该创建一个 &lt;optgroup&gt; 并带有 &lt;heads&gt; 中定义的 label
    • 根据给定的&lt;values&gt; 元素,每个&lt;optgroup&gt; 都填充有&lt;option&gt; 元素

    下面的代码是根据要求实现这个想法的jQuery方法。

    注意:$('test', xml)$(xml).find('test') 的简写。
    注意2:由于ID必须是唯一的,select#yourID等于#yourID
    注意 3:此脚本不需要 XML 中的 &lt;values&gt; 标签。

    success: function(xml){
        //Loop through each `<test>`
        $('test', xml).each(function(test_index){
            var id = $(this).attr('id'); //Get the ID of the `<test>`
            // Select <select> element with ID `ID` using the `select#ID` selector
            var select = $('select#' + id);
            $('optionheading', this).each(function() {
                $('values', this).each(function(){
                    var $values = $(this);
                    $('<option>')
                        .val($values.attr('images'))
                        .text($values.text())
                        .appendTo(optgroup);
                });
                $('<optgroup>')
                    .attr('label', $('heads', this).text())
                    .appendTo(select);
            });
        });
    }
    

    执行后可能出现的 HTML:

    <select id='1'>
        <optgroup label='Heading 1'>
            <option value='images/1.gif'>Option1</option>
            <option value='images/2.gif'>Option2</option>
            <option value='images/3.gif'>Option3</option>
        <optgroup>
        ....
    </select>
    <select id="2">
        ...
    

    【讨论】:

    • 其实我只需要处理第一个测试块。让我解释一下,我将测试块中的选项拉到下拉列表中,将另一个测试块拉到另一个下拉列表中,依此类推。这些下拉菜单相互依赖
    • 嘿 Rob,实际上我正在寻找代码的可重用性/可移植性。我不想每次添加东西时都更改我的 javacript。相反,我只想更新我的 xml 并让 jquery 处理它。就像我通过传递 id 在脚本中所做的那样
    • @Mike 那么,究竟你想要什么?是否要遍历 所有 &lt;test&gt; 元素并处理 &lt;optionheading&gt; 标记?
    • 我想处理下拉列表,一次进行一个测试,然后将它们放在一个选择下拉列表中(标题作为 optgroup,值作为选项)。每个选择下拉列表大约有 15 个选项和 5 个选择组。下一个下拉菜单有另外 15 个选项(标题作为 optgroup,值作为选项选项)。由于我的下拉列表取决于其他因素,所以一次我只想加载一个下拉列表(即一个测试块)。如果我需要另一个下拉菜单,那么我隐藏第一个下拉菜单并显示下一个下拉菜单,其选项来自 xml 的 test2 块。我希望我很清楚......谢谢你的帮助
    • @Mike 那么,您在 AJAX 调用之后填充每个 &lt;select&gt; 吗?目前,您的意图行为类似于“处理第一个 &lt;test&gt; 元素,并忽略其他元素”。
    猜你喜欢
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多