0

因此,我要做的只是根据 url 变量显示位于特定县的客户。例如http://www.example.com/clients.html?county=Ocean。我认为我的 if 语句没有正确完成。一些帮助将不胜感激!

XML

    <clients>    
        <client>
        <name>Client 1</name>
        <id>001</id>
        <counties>
        <county>Monmouth</county>
        <county>Ocean</county>
        </counties>
        </client>

        <client>
        <name>Client 2</name>
        <id>001</id>
        <counties>
        <county>Middlesex</county>
        <county>Ocean</county>
        </counties>
        </client>
   </clients>

jQuery

    $(document).ready(function()
    {
      $.ajax({
        type: "GET",
        url: "clients.xml",
        dataType: "xml",
        success: parseXml
      });
    });
    var county = $.getUrlVar('county');
    function parseXml(xml){

    $(xml).find("client").each(function(){
    if(county == $(this).find("county").text()) {

    $("#client").append('<div class="clients">' + ('<div class="name">' + $(this).find("name").text() + '</div>' + '</div>'); 

    });

    } 
else { alert("Fail!") }

    }

下面是我如何获取 url 变量以防万一

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});
4

0 回答 0