2015-01-31 74 views
0

更新如何从一个填补多个输入字段A HREF

我需要能够从我的实际XML文档中引用的XML,我不希望它只是var'd到jQuery的...

我要如何发生以下行为......

搜索标签和值的标签输入的搜索,然而,只有忽略从每个结果,以使打字阿拉巴马其各自的输入字段显示阿拉巴马州 - AL但只给我阿拉巴马州和AL值

也使用

$.ajax({ 
     type: "GET", 
     url: "states.xml", // change to full path of file on server 
     dataType: "xml", 
     success: parseXml, 
     complete: setupAC, 
     failure: function(data) { 
      alert("XML File could not be found"); 
      } 
    }); 

代替了var myXML

var myXml = '<?xml version="1.0" encoding="UTF-8"?><states><state label=Alabama value=AL country="US"><state label=Alaska value=AK country="US"></states>'; 
$(document).ready(function() { 
     var myArrLabel = []; 
     var myArrValue = []; 
     var myArrCountry = []; 

     function parseXml(xml){ 
      $(xml).find("state").each(function(){ 
       var a1=[], a2=[], a3=[]; 
       a1.push($(this).attr("label")); 
       a2.push($(this).attr("value")); 
       a3.push($(this).attr("country")); 
       $.each(a1, function(i, el){ 
        if($.inArray(el, myArrLabel) === -1) myArrLabel.push(el); 
       }); 
       $.each(a2, function(i, el){ 
        if($.inArray(el, myArrValue) === -1) myArrValue.push(el); 
       }); 
       $.each(a3, function(i, el){ 
        if($.inArray(el, myArrCountry) === -1) myArrCountry.push(el); 
       }); 
      }); 
     }; 
     parseXml(myXml); 

     function fillIfUnique(box1, box2, attr1, attr2) { 
      var value1 = box1.val(); 
      var valueItemsForLabel = $(myXml).find('state[' + attr1 + '="' + value1 + '"]'); 
      if (valueItemsForLabel.length) { 
       var value2 = valueItemsForLabel.eq(0).attr(attr2); 
       console.log('value2: ' + value2); 
       var totalSame = $(myXml).find('state[' + attr1 + '="' + value1 + '"][' + attr2 + '="' + value2 + '"]'); 
       if(valueItemsForLabel.length==totalSame.length) { 
        box2.val(value2); 
       } else { 
        box2.val(''); 
       }; 
      }; 
     }; 

     function setupAC() { 
      $("input#labelBox").autocomplete({ 
       source: myArrLabel, 
       minLength: 1, 
       select: function(event, ui) { 
        $("input#labelBox").val(ui.item.value); 
        fillIfUnique($('#labelBox'), $('#valueBox'), 'label', 'value'); 
        fillIfUnique($('#labelBox'), $('#countryBox'), 'label', 'country'); 
       } 
      }); 
      $("input#valueBox").autocomplete({ 
       source: myArrValue, 
       minLength: 1, 
       select: function(event, ui) { 
        $("input#valueBox").val(ui.item.value); 
        fillIfUnique($('#valueBox'), $('#labelBox'), 'value', 'label'); 
        fillIfUnique($('#valueBox'), $('#countryBox'), 'value', 'country'); 
       } 
      }); 
      $("input#countryBox").autocomplete({ 
       source: myArrCountry, 
       minLength: 1, 
       select: function(event, ui) { 
        $("input#countryBox").val(ui.item.value); 
        fillIfUnique($('#countryBox'), $('#labelBox'), 'country', 'label'); 
        fillIfUnique($('#countryBox'), $('#valueBox'), 'country', 'value'); 
       } 
      }); 
     }; 
     setupAC(); 
    }); 

    </script> 
<form name="search_form" id="searchForm" method="GET"> 
    <p><label for="labelBox">Label Search</label> 
     <input type="text" id="labelBox" name="labelBox" /></p> 
    <p><label for="valueBox">Value Search</label> <input type="text" id="valueBox" name="valueBox" /></p> 
    <p><label for="countryBox">Country Search</label> <input type="text" id="countryBox" name="countryBox" /></p> 

    <p><label></label> <button name="searchKeyword" id="searchKeyword">Submit</button></p> 
</form> 
+0

你可以请尝试创建一个小提琴,添加更多的代码,更具体吗?你的问题很混乱。 – skobaljic 2015-01-31 21:44:44

+0

更新@skobaljic – 2015-02-02 10:40:29

回答

0

我们必须遵循这样的逻辑:自动完成选择值后,我们要检查其他两个字段值是唯一他们的范围。例如,选择国家代码CA(XML value属性)具有独特的label加州,以及独特的country美国。如果这些值不唯一,那么我们将删除该输入值。检查功能名称是fillIfUnique(),请看this Fiddle

HTML使用:

<h3>jQuery Autocomplete using XML as Data Source Example</h3> 
<form name="search_form" id="searchForm" method="GET"> 
    <p><label for="labelBox">Label Search</label> 
     <input type="text" id="labelBox" name="labelBox" /></p> 
    <p><label for="valueBox">Value Search</label> <input type="text" id="valueBox" name="valueBox" /></p> 
    <p><label for="countryBox">Country Search</label> <input type="text" id="countryBox" name="countryBox" /></p> 

    <p><label></label> <button name="searchKeyword" id="searchKeyword">Submit</button></p> 
</form> 

脚本:

$(document).ready(function() { 
    var myArrLabel = []; 
    var myArrValue = []; 
    var myArrCountry = []; 

    function parseXml(xml){ 
     $(xml).find("state").each(function(){ 
      var a1=[], a2=[], a3=[]; 
      a1.push($(this).attr("label")); 
      a2.push($(this).attr("value")); 
      a3.push($(this).attr("country")); 
      $.each(a1, function(i, el){ 
       if($.inArray(el, myArrLabel) === -1) myArrLabel.push(el); 
      }); 
      $.each(a2, function(i, el){ 
       if($.inArray(el, myArrValue) === -1) myArrValue.push(el); 
      }); 
      $.each(a3, function(i, el){ 
       if($.inArray(el, myArrCountry) === -1) myArrCountry.push(el); 
      }); 
     }); 
    }; 
    parseXml(myXml); 

    function fillIfUnique(box1, box2, attr1, attr2) { 
     var value1 = box1.val(); 
     var valueItemsForLabel = $(myXml).find('state[' + attr1 + '="' + value1 + '"]'); 
     if (valueItemsForLabel.length) { 
      var value2 = valueItemsForLabel.eq(0).attr(attr2); 
      console.log('value2: ' + value2); 
      var totalSame = $(myXml).find('state[' + attr1 + '="' + value1 + '"][' + attr2 + '="' + value2 + '"]'); 
      if(valueItemsForLabel.length==totalSame.length) { 
       box2.val(value2); 
      } else { 
       box2.val(''); 
      }; 
     }; 
    }; 

    function setupAC() { 
     $("input#labelBox").autocomplete({ 
      source: myArrLabel, 
      minLength: 1, 
      select: function(event, ui) { 
       $("input#labelBox").val(ui.item.value); 
       fillIfUnique($('#labelBox'), $('#valueBox'), 'label', 'value'); 
       fillIfUnique($('#labelBox'), $('#countryBox'), 'label', 'country'); 
      } 
     }); 
     $("input#valueBox").autocomplete({ 
      source: myArrValue, 
      minLength: 1, 
      select: function(event, ui) { 
       $("input#valueBox").val(ui.item.value); 
       fillIfUnique($('#valueBox'), $('#labelBox'), 'value', 'label'); 
       fillIfUnique($('#valueBox'), $('#countryBox'), 'value', 'country'); 
      } 
     }); 
     $("input#countryBox").autocomplete({ 
      source: myArrCountry, 
      minLength: 1, 
      select: function(event, ui) { 
       $("input#countryBox").val(ui.item.value); 
       fillIfUnique($('#countryBox'), $('#labelBox'), 'country', 'label'); 
       fillIfUnique($('#countryBox'), $('#valueBox'), 'country', 'value'); 
      } 
     }); 
    }; 
    setupAC(); 
}); 

注:我不得不压缩并插入XML脚本进入。我删除了数组中的重复条目。

+0

如何将其链接到外部XML源? – 2015-02-02 12:14:27

+0

也有可能通过标签和值进行搜索,在建议的结果上显示它们,但只将它们各自的值放入输入字段中? – 2015-02-02 12:33:10

+0

对不起,这是否有可能为第一个字段搜索标签,但也显示其价值,所以键入wyom将显示怀俄明州 - 怀俄明州,但只把onClick到每个单独的输入?还有如何链接外部XML – 2015-02-02 12:46:24

相关问题