Home Web Front-end HTML Tutorial Detailed explanation of Html5 datalist tag and dynamic matching with background data

Detailed explanation of Html5 datalist tag and dynamic matching with background data

May 22, 2017 am 10:15 AM
html5

HTML5’s new tag datalist enables automatic entry into the database fuzzy query when entering the first letter of Chinese/Pinyin, and returns the corresponding results to generate a datalist. When the input content in the input box changes, the datalist will automatically trigger the drop-down. Frame, the solution is very good. Let me share the example code with you through this article. Friends who need it can refer to it

The recent project involves a small function. When customers choose suppliers, due to the large number of suppliers, There are many (about 3,000), so it is obviously unrealistic to directly generate drop-down boxes, so I changed the solution and planned to use the new tag datalist in HTML5 to automatically enter the database fuzzy query when entering the first letter of Chinese/Pinyin and return the corresponding As a result, a datalist is generated. Since the datalist will automatically trigger the drop-down box when the input content in the input box changes, it is more convenient to use than select. The front-end code is as follows:

Html Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

<!DOCTYPE html> 

<html lang="en"

  <head id="head"

    <title>库存下拉框测试</title> 

    <meta charset="utf-8"

    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible"

    <meta name="viewport" content="width=device-width, initial-scale=1.0"

    <meta name="description" content=""

    <meta name="author" content=""

    <script src="../../Common/content/jquery-1.7.2.min.js"type="text/javascript"></script> 

    <script src="../../Common/pages/include.js" class="include" type="text/javascript"></script> 

    <script src="../../Common/js/AjaxJson.js"  type="text/javascript"></script> 

    <script src="../../Common/js/Setting.js"  type="text/javascript"></script> 

    <script src="../../Common/js/Paging.js"  type="text/javascript"></script> 

    <script src="../../Js/warehouseManage/testyy.js" type="text/javascript"></script>  

  </head> 

  <body class=""

    <!--<![endif]--> 

    <p class="navbar"></p> 

    <p class="sidebar-nav"></p> 

    <p class="content"

        <p class="header"><h1 id="ADU" class="page-title">下拉框测试</h1></p> 

        <p class="container-fluid"

            <p class="row-fluid"

                <!-- --------------------------多条件查询--------------------------------------------- --> 

                <p class="well" id="searchDemo"

                <p>测试数据(默认均为d00001):<br>    昆山市大陆配件有限公司     ksdlpjyxgs <br> 

                            亿真企业有限公司                        yzqyyxgs        <br> 

                            泰州市安誊轴皮厂(集团厂)   tzsatzpc(jtc) 

                </p>   

                </p> 

                    按 供应商名动态匹配(中文或者拼音均可): 

                    <input list="bro"  id="name"   oninput="this.value=this.value.replace(/^ +| +$/g,&#39;&#39;);search(&#39;name&#39;,&#39;bro&#39;,&#39;name&#39;)" 

                    <datalist  id="bro"></datalist>  

            </p> 

        </p> 

    </p>  

    </body>    

                <!-- -----------------------footer-------------------------- --> 

                <footer  class="foot"></footer> 

</html>

Copy after login

JavaScript Code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

var listobj=null;            //datalist对象 

var requestItem=null;        //后台返回的json数据中所需的key值 

var inputContent=null;       //input标签对象 

/**search()说明:

 * inputID:     input标签的ID

 * datalistID:  datalist标签的ID

 * itemName:    后台返回的json数据中所需的key值(仅需表格中中文字段的属性名)

 * */ 

function search(inputID,datalistID,itemName) 

    inputContent=document.getElementById(inputID); 

    var datalist=document.getElementById(datalistID); 

    //防止在无输入内容的情况下产生遗留下拉选项 

    if(inputContent.value.length==0||inputContent.value==" "

    {    

        var sub=datalist.childNodes; 

        if(sub.length>0) 

        

            for (var i =sub.length-1; i>=0 ; i--)  

            

                datalist.removeChild(sub[i]);        

            

        

        listobj=null;            

        requestItem=null;         

        inputContent.value=null; 

        return false; 

    

    //全局变量赋值 

    listobj=datalist; 

    requestItem=itemName; 

    var data=""

    var url="";  

    if(/^[a-zA-Z]*$/.test(inputContent.value)) 

    

        //检测出是拼音首字母 

        data="type=searchWords¶m="+inputContent.value;      //注意:data-----------需要自定义 

        url=baseurl + "/servlet/ListDemo";                      //注意:url-----------需要自定义 

        sendRequest("post",url,data,getResult); 

    

    else if (/^[\u4e00-\u9fa5]*$/.test(inputContent.value)) 

    

        //检测出是中文 

        data="type=searchChinese¶m="+inputContent.value;    //注意:data-----------需要自定义 

        url=baseurl + "/servlet/ListDemo";                      //注意:url-----------需要自定义 

        sendRequest("post",url,data,getResult); 

    

//填写仓库下拉框 

function getResult(result)  

    var data=result; 

    var JData=eval("(" + data + ")"); 

    var maxlength=10;              //注释:maxlength保证过多查询结果下只显示10条 

    if(JData.length<=10) 

    {   

        maxlength=JData.length;           

    

    var sub=listobj.childNodes; 

    for (var i =sub.length-1; i>=0 ; i--)  

    

        listobj.removeChild(sub[i]);    //清空datalist所有的下拉选项  

    

    if(JData.length==0)  //没有查询结果 

    

        alert("没有符合条件的结果,请重输"); 

        inputContent.value="";    //清空input输入框的值 

        return false; 

    

    for (var i=0;i<maxlength;i++)  

    

         var obj=document.createElement("option"); 

         var indexobj=JData[i]; 

        if(/^[a-zA-Z]*$/.test(inputContent.value)) 

        {  

             obj.value=indexobj[requestItem]; 

             obj.innerHTML=inputContent.value; 

        

        if (/^[\u4e00-\u9fa5]*$/.test(inputContent.value)) 

        

             obj.value=indexobj[requestItem]; 

        }  

         listobj.appendChild(obj);      

    

    var suffix=document.createElement("option"); 

    suffix.value=" "

    suffix.innerHTML="输入更多有关"+inputContent.value+"的信息"

    listobj.appendChild(suffix); 

    return false; 

}

Copy after login

[Related recommendations]

1. HTML free video tutorial

2. Share a super comprehensive summary of HTML and CSS knowledge points

3. Teach you how to parse html under nodejs

4. htmlDetailed explanation of the example of implementing the quantity subscript on the message button

5. How to display JSON data in html introduce

The above is the detailed content of Detailed explanation of Html5 datalist tag and dynamic matching with background data. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles