Avatar billede bhn.314 Nybegynder
12. august 2004 - 09:39 Der er 4 kommentarer og
1 løsning

global værdi problem (onBlur vs onFocus)

Jeg kalder en function der søger efter en bestem værdi i en dropdown. Skriver man "Kø" (onKeyDown) i dropdownen finder den eksempelvis "København". Function'en bruges i mange drop-downs på siden. Jeg har brug for at gemme det der indtastes så længe jeg er i den enkelte drop-down. Men går jeg til en anden så skal den globale værdi reset'es. Jeg kan IKKE bruge onBlur i HTML'en da funktionen blur'er dropdownen.

var Typed="";

//Kaldes via onKeyDown i HTML
function KeyPress(thePerson, theDrop)
{
//bla.. bla.....
theTyped+=String.fromCharCode(event.keyCode).toLowerCase();
//bla.. bla.....
}

<select name="drop1" onKeyDown="KeyPress(person, this);">

//Virker ikke da theTyped har samme værdi som det der er tastet i drop1
<select name="drop2" onKeyDown="KeyPress(person, this);">
Avatar billede bhn.314 Nybegynder
12. august 2004 - 09:40 #1
Hele scriptet:

// This is a global value to determine which item to select
var selectedOption=0;
var KeyPressEnabled;
var theTyped="";

function SelectChange(Drop1)
{
  var KeyPressEnabled = true;
  // We use this function to disable any dynamic selecting of the
  // combo-box when the user uses the mouse to select items.
  if (KeyPressEnabled)
  {
      Drop1.selectedIndex = selectedOption; 
  }
}
function ClearTyped()
{
      theTyped=""; 
}

function KeyPress(thePerson, theDrop)
{
  var Drop1= theDrop;
  var main = thePerson;

  KeyPressEnabled = true;

  // Keycode 38 and 40 are the up and down arrow buttons, in case the user
  // uses the arrow keys to select items. We reset the value of what they
  // have typed and change the global selected item value.
    if (event.keyCode == 38)
  {
      theTyped="";
      selectedOption = Drop1.selectedIndex - 1;
      return;
  }

  if (event.keyCode == 40)
  {
      theTyped="";
      selectedOption = Drop1.selectedIndex + 1;
      return;
  }

  // Keycode 27 is escape, so the user can clear out what
  // they have typed so far
  if (event.keyCode == 27)
  {
      Drop1.selectedIndex = 0;
      theTyped="";
      return;
  }

    // Keycode 32 is a space
    if (event.keyCode != 32)
    {
        if ((event.keyCode < 65 || event.keyCode > 90) && !(event.keyCode == 27 || event.keyCode==222 || event.keyCode==221 ||
          event.keyCode==192))
        {
            return;
        }
    }
 
  // Convert the ASCII keycode value to a character and add the key
  // entered to a "buffer". Normally, this would be a hidden field.
  if(event.keyCode==222)
  {
      theTyped+="ø";
  }
  else if(event.keyCode==221)
  {
      theTyped+="å";
  }
  else if(event.keyCode==192)
  {
      theTyped+="æ";
  }
  else if(event.keyCode==27)
  {
      theTyped+="";
  }
  else
  {
      theTyped+=String.fromCharCode(event.keyCode).toLowerCase();
  }

  setTimeout("",100);//put some delay because select's behavior

  // Loop through the select list to find any matches 
  for (x = 0; x < Drop1.length; x++)
  {
      var tmpOptionText = "";
      var OptionText = Drop1.options[x].text;
   
      // Loop through the value of each select item, and if there is a
      // match on what they have typed in, set the global variable for that value.
      // The browser will run the onChange event since you typed a key. We'll
      // run SelectChange() in the onChangeEvent just to be sure.
      for (y = 0; y < OptionText.length; y ++)
      {
        tmpOptionText += OptionText.charAt(y).toLowerCase();

      if (tmpOptionText == theTyped)
        {
            Drop1.selectedIndex = x;
            selectedOption = x;
            Drop1.focus();
            return;
        }
      }
  }
}

HTML:

<select name="drop1" onKeyDown="KeyPress(person, this);">
//Virker ikke da theTyped har samme værdi som det der er tastet i drop1
<select name="drop2" onKeyDown="KeyPress(person, this);">
Avatar billede roenving Novice
12. august 2004 - 14:33 #2
Hvis du gør den til et array kan de tilhøre hver sin dropdown, men hvad så, hvis man vender tilbage ?-)

<script type="text/javascript">
var selectedOption=0;
var KeyPressEnabled;
var theTyped = new Array();

function SelectChange(Drop1)
{
  var KeyPressEnabled = true;
  // We use this function to disable any dynamic selecting of the
  // combo-box when the user uses the mouse to select items.
  if (KeyPressEnabled)
  {
      Drop1.selectedIndex = selectedOption; 
  }
}
function ClearTyped(id)
{
      theTyped[id]=""; 
}

function KeyPress(thePerson, theDrop)
{
  var Drop1= theDrop;
  id = theDrop.name;
  var main = thePerson;
  if(!theTyped[id])theTyped[id]="";

  KeyPressEnabled = true;

  // Keycode 38 and 40 are the up and down arrow buttons, in case the user
  // uses the arrow keys to select items. We reset the value of what they
  // have typed and change the global selected item value.
    if (event.keyCode == 38)
  {
      theTyped[id]="";
      selectedOption = Drop1.selectedIndex - 1;
      return;
  }

  if (event.keyCode == 40)
  {
      theTyped[id]="";
      selectedOption = Drop1.selectedIndex + 1;
      return;
  }

  // Keycode 27 is escape, so the user can clear out what
  // they have typed so far
  if (event.keyCode == 27)
  {
      Drop1.selectedIndex = 0;
      theTyped[id]="";
      return;
  }

    // Keycode 32 is a space
    if (event.keyCode != 32)
    {
        if ((event.keyCode < 65 || event.keyCode > 90) && !(event.keyCode == 27 || event.keyCode==222 || event.keyCode==221 ||
          event.keyCode==192))
        {
            return;
        }
    }
 
  // Convert the ASCII keycode value to a character and add the key
  // entered to a "buffer". Normally, this would be a hidden field.
  if(event.keyCode==222)
  {
      theTyped[id]+="ø";
  }
  else if(event.keyCode==221)
  {
      theTyped[id]+="å";
  }
  else if(event.keyCode==192)
  {
      theTyped[id]+="æ";
  }
  else if(event.keyCode==27)
  {
      theTyped[id]+="";
  }
  else
  {
      theTyped[id]+=String.fromCharCode(event.keyCode).toLowerCase();
  }

  setTimeout("",100);//put some delay because select's behavior

  // Loop through the select list to find any matches 
  for (x = 0; x < Drop1.length; x++)
  {
      var tmpOptionText = "";
      var OptionText = Drop1.options[x].text;
   
      // Loop through the value of each select item, and if there is a
      // match on what they have typed in, set the global variable for that value.
      // The browser will run the onChange event since you typed a key. We'll
      // run SelectChange() in the onChangeEvent just to be sure.
      for (y = 0; y < OptionText.length; y ++)
      {
        tmpOptionText += OptionText.charAt(y).toLowerCase();

      if (tmpOptionText == theTyped[id])
        {
            Drop1.selectedIndex = x;
            selectedOption = x;
            Drop1.focus();
            return;
        }
      }
  }
}
</script>
Avatar billede bhn.314 Nybegynder
18. august 2004 - 11:51 #3
roenving >>> Tak for dit input det var guld værd. Du reddede min dag ! Smid et svar og få point.
Avatar billede roenving Novice
18. august 2004 - 12:06 #4
Velbekomme '-)
Avatar billede roenving Novice
01. november 2004 - 15:22 #5
-- og tak for point ;~}
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester