简体   繁体   English

javascript-如何从自动完成功能获取onChange事件的完整值

[英]javascript - how to get complete value for onChange event from auto-complete function

First of all, I am new to Javascript. 首先,我是Java的新手。 I need concrete help not only explanations of what to do. 我不仅需要解释如何做,还需要具体的帮助。 Thx! 谢谢!

Description of the problem: 问题描述:

I have a text field which after the user starts typing in it, generates a drop down list with values out of the database....basically an auto complete function. 我有一个文本字段,在用户开始输入之后,该字段会生成一个包含数据库值的下拉列表。...基本上是一个自动完成功能。

Based on the value in this text field, another process needs to be initiated using the value. 根据此文本字段中的值,需要使用该值来启动另一个过程。 But the problem is that the value is only what the user typed in the field, not what the user eventually picked out of the auto complete list. 但是问题在于该值仅是用户在字段中键入的值,而不是用户最终从自动完成列表中选择的值。

example: 例:

I type in "bo" and the auto complete list generates "boeing". 我输入“ bo”,自动完成列表将生成“ boeing”。 I click on boeing to fill in the field... but the value picked up is "bo". 我单击波音以填写该字段...但是获取的值为“ bo”。

How can I capture the complete value and use it in an onChange event explained below? 如何捕获完整值并在下面说明的onChange事件中使用它?

The concrete stuff: 具体的东西:

1)the fields: 1)领域:

<input type=\"text\" size=\"30\" name=\"manuList\"
    id=\"inputString\" value=\"$_POST[manu_prev]\" 
    onkeyup=\"lookup(this.value);\" onblur=\"fill();\" 
    onChange=\"htmlData('get_model.php', 'ch='+this.value)/>

<div class=\"suggestionsBox\" id=\"suggestions\" 
     style=\"display: none;\">
  <img src=\"/images/upArrow.png\" 
    style=\"position: relative; top: -12px; 
            left: 30px;\" alt=\"upArrow\" />
  <div class=\"suggestionList\" id=\"autoSuggestionsList\">
      &nbsp;</div>
</div>

2) the function htmlData needs the value of the field (onChange event), but as explained above, only what the user types is stored in this.value, not what gets picked from the list. 2)函数htmlData需要字段的值(onChange事件),但是如上所述,只有用户键入的内容存储在this.value中,而不是从列表中选择的内容。 I want to print the value picked by the user by this: onChange=\\"htmlData('get_model.php', 'ch='+this.value) But instead of printing the complete value "boeing" it will print "bo" which is what the user typed to generate the list. 我想通过以下方式打印用户选择的值:onChange = \\“ htmlData('get_model.php','ch ='+ this.value)但不是打印完整的值” boeing“,而是打印” bo“用户键入生成列表的内容。

function htmlData(url, qStr)
{
   if (url.length==0)
   {
       document.getElementById("txtResult").innerHTML="";
       return;
   }
   xmlHttp=GetXmlHttpObject()
   if (xmlHttp==null)
   {
       alert ("Browser does not support HTTP Request");
       return;
   }

   url=url+"?"+qStr;
   url=url+"&sid="+Math.random();
   xmlHttp.onreadystatechange=stateChanged;
   xmlHttp.open("GET",url,true) ;
   xmlHttp.send(null);
}

The initiated php script "get_model.php" prints the value: 启动的php脚本“ get_model.php”显示值:

 echo $_GET['ch'];  

The other functions used for the auto complete: 自动完成的其他功能:

function lookup(inputString)
{
  if(inputString.length == 0)
  {
    // Hide the suggestion box.
    $('#suggestions').hide();
  } else
  {
    $.post("rpc.php", {queryString: ""+inputString+""}, function(data)
    {
      if(data.length >0) 
      {
        $('#suggestions').show();
        $('#autoSuggestionsList').html(data);
      }
    });
  }
} // lookup

function fill(thisValue) 
{
  $('#inputString').val(thisValue);
  setTimeout("$('#suggestions').hide();", 200);
}

function GetXmlHttpObject(handler)
{
   var objXMLHttp=null
   if (window.XMLHttpRequest)
   {
       objXMLHttp=new XMLHttpRequest()
   }
   else if (window.ActiveXObject)
   {
       objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
   }
   return objXMLHttp
}

function stateChanged()
{
   if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
   {
       document.getElementById("txtResult").innerHTML= xmlHttp.responseText;
   }
   else 
   {
       //alert(xmlHttp.status);
   }
}

You need to listen to the event on the list you create - can you give an example of the output that fills the autoSuggestionsList 您需要在创建的列表上侦听事件-您可以举一个填充autoSuggestionsList的输出示例autoSuggestionsList

For example it might be a list that is added by the following line : 例如,它可能是由以下行添加的列表:

$('#autoSuggestionsList').html(data);

that could give you 那可以给你

<div class=\"suggestionList\" id=\"autoSuggestionsList\">
      <ul id="unOrderedList">
          <li value="2">Whatever</li>
          ......
      </ul>
</div>

you can then setup an event listener on the <li> 's... 然后可以在<li>的...上设置事件监听器。

$('#unOrderedList li').click(function(){
      var value = $(this).attr('value');
      // Do what every you want with the data.....
  });

This would replace the onchange event on the input field 这将替换输入字段上的onchange事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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