简体   繁体   中英

Javascript: unique location.href

Hi,

I am currently working on this autocomplete-searchbox, and as untutored I am when it comes to JavaScript i wonder: how do i give each value an specific unique link to another .html page? So that 'Desserts' links to page1.html & 'Snacks' to page2.html ?

As you can see are all the values currently linking to location.href = " http://www.cnn.com "; but i want to give each value a specific location.href..

Best regards

$(function(){

   var term = [  

    { value: 'Desserts' },

    { value: 'Snacks'},

    { value: 'Drinks'},

    { value: 'Cheesecake'},

    { value: 'Cookies'},

  ];

  $('#autocomplete').autocomplete({
    lookup: term,

  onSelect: function myFunction() {
    location.href = "http://www.cnn.com";
}
  });
});

I think this is the direction you want to head. If you post more of what you have I can give you a more specific answer

 $(function(){
   var term = [{ 
     value: 'Desserts', 
     location: 'page1.html' 
  },{ 
     value: 'Snacks', 
     location: 'page2.html' 
  }, { 
     value: 'Drinks', 
     location: 'page3.html' 
  }, { 
     value: 'Cheesecake', 
     location: 'page4.html'
  }, { 
     value: 'Cookies', 
     location: 'page5.html'
  },];

$('#autocomplete').autocomplete({
  lookup: term,
  onSelect: function myFunction(e) {
    //Depending on how you trigger onSelect e.currentTarget.location 
    //may or may not work. but you should be able to start with e 
    //and work your way to finding location.
    location.href = "http://www.cnn.com"+e.currentTarget.location;
  }
});
});

Also you could look into a library with a built in autocomplete and the documentation that goes with it. JQuery have something you could look into here: http://jqueryui.com/autocomplete/#combobox

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM