简体   繁体   English

自动完成代码?

[英]Codes for autocomplete?

I am currently trying to write some codes to autocomplete a field. 我目前正在尝试编写一些代码以自动完成字段。 I have actually completed around 90% and it works until now. 我实际上已经完成了大约90%,并且一直有效到现在。

When I want to search people by last name in the database, starting by 's' for example, and I type 's' in the Enter Last Name field, all last names starting by s is displayed. 当我要在数据库中按姓氏搜索人员时(例如以“ s”开头),并且在“输入姓氏”字段中键入“ s”时,将显示所有以s开头的姓氏。 I am satisfied with the result so far. 到目前为止,我对结果感到满意。 Please see the picture below: 请看下面的图片:

在此处输入图片说明

However, I cannot write the appropriate codes which will allow the field 'Enter last Name' to be filled with the name when the user clicks on a name in the drop-down. 但是,我无法编写适当的代码,当用户单击下拉列表中的名称时,该名称将不允许在“输入姓氏”字段中填充名称。

Could somebody help me? 有人可以帮我吗? What codes can I use to perform the task I described above? 我可以使用什么代码执行上述任务?

I have used the codes below for each row in the drop-down. 我在下拉菜单的每一行中都使用了以下代码。 rs.getString(1) represents the name which has been retrieved from a MySQL database. rs.getString(1)代表已从MySQL数据库检索的名称。

<div onclick='fill(rs.getString(1));'>rs.getString(1)</div>

Furthermore, in the head of the html page, I have declared a javascript function as follows: 此外,在html页面的开头,我声明了一个javascript函数,如下所示:

<script type="text/javascript">

function fill(thisValue) {
           $('#inputString').val(thisValue);
}

</script>

I was thinking that when the user clicks on a name in the drop-down, the fill function would be called and the parameter thisValue of the function would be set to rs.getString(1). 我在想,当用户在下拉列表中单击一个名称时,将调用fill函数,并将该函数的参数thisValue设置为rs.getString(1)。 Finally the value of the text field Enter Last Name which has the attribute id=inputString would be set to rs.getString(1). 最后,将具有id = inputString属性的文本字段Enter Last Name的值设置为rs.getString(1)。 And hence I would be able to populate the text field Enter Last Name based on what the user clicks. 因此,我将能够根据用户单击的内容填充“输入姓氏”文本字段。

However, nothing happens when I click on a name in the drop-down. 但是,当我单击下拉列表中的名称时,没有任何反应。 The field Enter Last Name is not filled with any data. 输入姓氏字段未填充任何数据。 I have to manually input the names and it defeats the purpose of the autocomplete system. 我必须手动输入名称,这违反了自动完成系统的目的。

Could somebody please guide me? 有人可以指导我吗?

Thanks for reading. 谢谢阅读。

Regards 问候

Assuming the list is a <ul class='list'><li>... : 假设列表是<ul class='list'><li>...

// on clicking the li of ul class .list
$('.list').on('click', 'li', function() {
    // put the text of the ul to the value of the input
    $('#inputString').val(this.text());
});

Add a class to the div as follow. 如下向div添加一个类。

<div class='row'>rs.getString(1)</div>

And now in the Jquery use the following code. 现在在Jquery中使用以下代码。

$(".row").click(function(e){
       $("#inputstring").val($(this).html());
 });

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

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