admin管理员组

文章数量:1292594

Hi to all, I am new to programming. will you help me to write code for autoplete text fields in a html form. I want to use local storage data. If any time user will inter some data in text field, it will be stored in local storage. if next time when he enters data then, localstorage data related to that field will appear as popup(like mozila or chrome autoplete).

please provide me some guidelines

Hi to all, I am new to programming. will you help me to write code for autoplete text fields in a html form. I want to use local storage data. If any time user will inter some data in text field, it will be stored in local storage. if next time when he enters data then, localstorage data related to that field will appear as popup(like mozila or chrome autoplete).

please provide me some guidelines

Share Improve this question edited Jun 20, 2011 at 16:23 love Computer science 1,8284 gold badges20 silver badges39 bronze badges asked Mar 18, 2011 at 11:13 prabhatprabhat 191 silver badge1 bronze badge 1
  • and what have you tried so far? – bluefoot Commented Mar 18, 2011 at 11:15
Add a ment  | 

3 Answers 3

Reset to default 7

You can use <datalist> tag of html5 to use it as autoplete. Here is an example:

<form action="action.php" method="get">

  <input list="bdcity_list" name="bdcity" />
  <datalist id="bdcity_list">
     <option value="Dhaka">
     <option value="Khulna">
     <option value="Sylhet">
     <option value="Rajshahi">
     <option value="Bandarbon">
  </datalist>
 <input type="submit" />

</form>

You can use it like this:

<input list="Employees" type="text" />
<datalist id="Employees">
<option value="Adarsh"></option>
<option value="Madhukar"></option>
<option value="Amar"></option>
<option value="Avinash"></option>
<option value="Kundan"></option>
<option value="Amit"></option>
<option value="Viresh"></option>
<option value="Vivek"></option>
</datalist>

for further info go to http://madhukar.info/Pages/Articles.aspx?ArtId=49

Well, in order to get the results you desire, I would probably first build a JavaScript object out of the data that the user has entered into the form. This will make it easier to enter the data into the local storage. Say your form looks like this ->

<html>
  <form action='someaction.php'>
    name: <input id='name' type='text' />
    age : <input id='age' type='text' />
    <input type='submit' id='submit' value='submit' />
  </form>

  <ul id='autoplete'>

  </ul>

</html>

There is a great guide to working with HTML5 local storage here, and should help you to understand the key-value pair system that it expects you to use for storing data. Assuming you were using jQuery, you could prepare and send your data like this

$('#submit').click(function(){
  // first get the proper values
  var name = $('#name').val();
  var age = $('#age').val();

  // next create the object
  var obj = {name:name,age:age}

  // next send the object off to localstorage
  // using the localStorage object, we can
  // very simply pass in data
  localStorage.setItem('user',obj);
});

As far as the autoplete portion of your problem goes, there are a few different ways you could go about acplishing this. As someone mentioned, you could go for jQuery autoplete, but I'm not too familiar with its API, so I would probably go for some plain jane JavaScript. The code would also depend on whether you were checking against multiple values. However in the simplest case, remembering a single user's data, All you'd really need to do is check for the existence of the value in local storage. Something like this possibly ->

$('#name').change(function(){
  // check the local storage to see if
  // there are similar values to what's
  // being entered
  var currentText = $('#name').val();

  if( localStorage.user.indexOf(currentText) == 0){
    $('#name').val(localStorage.user);
  }
});

This was all written directly into this textbox at StackOverflow, so it's not intended to run as written and I'm not in any place to test this. I do, however, hope this can move you in the right direction.

本文标签: javascripthow to create autocomplete forum using html5 local storageStack Overflow