Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html AJAX,从XML文件填充下拉菜单_Html_Ajax_Xml - Fatal编程技术网

Html AJAX,从XML文件填充下拉菜单

Html AJAX,从XML文件填充下拉菜单,html,ajax,xml,Html,Ajax,Xml,我正在做一个项目,我不知道下一步该做什么。我的目标是用xml文件填充下拉菜单。菜单基于4个步骤。“Fylke”“Kommune”“Kategori”“Dato”。现在,我使用下面的示例获取xml以将数据提供给“Fylke”。下一步是当用户从“Fylke”菜单中选择Akershus时,用Halden填充“Kommune” 以下是XML: <arrangement> <fylke name="Akershus"> <kommune name="Halden"

我正在做一个项目,我不知道下一步该做什么。我的目标是用xml文件填充下拉菜单。菜单基于4个步骤。“Fylke”“Kommune”“Kategori”“Dato”。现在,我使用下面的示例获取xml以将数据提供给“Fylke”。下一步是当用户从“Fylke”菜单中选择Akershus时,用Halden填充“Kommune”

以下是XML:

<arrangement>
   <fylke name="Akershus">
   <kommune name="Halden">
   <arrangement id="570215856">
   <kategori>Moro</kategori>
   <dato>2013-10-10</dato>
</arrangement>
”;
$('ul')。追加(信息);
});
},
错误:函数(){$('ul').children().remove();
$('ul').append(“
  • 发生错误!
  • ”);} }); }
    您正在使用jquery吗

    如果是这样,您需要将单击事件附加到外部ul,这可以在就绪功能中完成

    $( "ul li" ).click(function() {
      alert( "Handler for .click() called." );
    });
    

    您当前未将li项目添加到ul中

    您确实需要为要更新的元素提供ID,以便使用

    $(" #myid li ").append(info);
    

    否则,您将向表单上的所有ul元素添加信息

    不确定我是否正确处理了您的问题,但希望这有助于:

    $(xml).find("arrangement fylke").each( function(i) {
        var info = '<a href="#" nodeno="'+i+'" >'+$(this).attr("name")+'</a>'; //attribute nodeno added, carries the number of the node you will need later
        $('ul').append(info); 
    });
    
    $('ul').on("click", function(e){ //when clicked on one of those elements
        var nodeno = $(e.target).attr("nodeno"); //get nodeno attribute from clicked object
        $(xml)[nodeno].find("arrangement kommune").each( function(i){ //get the node by id and...
            var info = '<a href="#" nodeno="'+i+'" >'+$(this).attr("name")+'</a>';
            $('#menuitem2').append(info); //...populate your submenu with the kommune object
        } );
    });
    
    $(xml).查找(“排列fylke”).每个(函数(i){
    var info='';//添加了属性nodeno,携带稍后需要的节点编号
    $('ul')。追加(信息);
    });
    $('ul')。在(“单击”)上,单击其中一个元素时,函数(e){//
    var nodeno=$(e.target).attr(“nodeno”);//从单击的对象获取nodeno属性
    $(xml)[nodeno].find(“arrangement kommune”).each(function(i){//通过id获取节点,然后。。。
    var信息=“”;
    $(“#menuitem2”).append(info);/…使用kommune对象填充子菜单
    } );
    });
    
    编辑:一点解释

    这将产生fylke链接,如:

    <a href="#" nodeno="0">Fylke 1</a>
    <a href="#" nodeno="1">Fylke 2</a>
    
    
    
    单击其中一个节点时,您只需从单击的元素中读取nodeno属性,并使用该属性从xml中获取相应的节点。然后,您只能从该节点使用kommune来填充下一个菜单级别


    希望我正确地解决了您的问题,这会有所帮助

    好的,非常感谢。但是我不知道在我的代码中应该把你的例子放在哪里?我让你的例子工作,但我做的和我的一样,当我点击“Fylke”中的一个项目时,什么都没有发生。现在它可以工作了,但只有当我从$(xml)[nodeno]中删除[nodeno]时,才能找到。然后输入菜单中所有的“Kommune”名称。不是xml中fylke节点内的。Firebug说nodeno不是一个函数。我很困惑。但是差不多了:)也许一点解释会有帮助:您需要减少作为第二次“查找”基础的xml,所以这就是nodeno的意图。“$(xml)[nodeno]。查找(…”应该只在为kommune单击的fylke的nodeno中搜索。您可以在多个fylke和commune节点上附加一点xml吗?再次感谢。这确实很有帮助。菜单现在已完成:)谢谢,我将尝试此操作。
    <a href="#" nodeno="0">Fylke 1</a>
    <a href="#" nodeno="1">Fylke 2</a>