Home Web Front-end JS Tutorial Ajax implements the effect of changing the text of the input box to display the drop-down list

Ajax implements the effect of changing the text of the input box to display the drop-down list

Jul 02, 2018 pm 04:15 PM
ajax drop-down list Input box

This article mainly introduces the effect of changing the input box text and displaying the drop-down list through ajax. Friends in need can refer to

1. Style

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

<style type="text/css">

<!--

body{background:#fff}

.Menu {

position:relative;

width:180px;

height:120px;

z-index:1;

background: #EEE;

border:1px solid #666;

margin-top:-100px;

display:none;

}

.Menu2 {

position: absolute;

left:0;

top:0;

width:100%;

height:120px;

overflow:hidden;

z-index:1;

OVERFLOW-y:auto;

}

.Menu2 ul{margin:0;padding:0}

.Menu2 ul li{width:100%;height:25px;line-height:20px;text-indent:15px;

border-bottom:1px dashed #999;color:#333;cursor:pointer;

change:expression(

this.onmouseover=function(){

this.style.background="";

},

this.onmouseout=function(){

this.style.background="";

}

)

}

input{width:120px}

#List1,#List2{left:0px;top:103px;}

-->

</style>

Copy after login

2. HTML script

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

........省略常规脚本

<tr>

<th>汽车品牌名:</th>

<td>

<input type="text" id="generalBrandName" name="generalBrandName" value="${*.generalBrandName}" style="width:180px" data-validation-engine="validate[required]" <c:if test="${!empty carType.brandIdGeneral}"> disabled="disabled" </c:if> onfocus="showAndHide(&#39;List1&#39;,&#39;show&#39;);" onblur="showAndHide(&#39;List1&#39;,&#39;hide&#39;);"/>

<input type="hidden" id="brandIdGeneral" name="brandIdGeneral" value="${*.brandIdGeneral}" style="width:180px" />

<span class="required">必填*</span>

<p class="Menu" id="List1">

<p class="Menu2" id="ListLi1">

<%-- <ul>--%>

<%-- <li onmousedown="getValue(&#39;generalBrandName&#39;,&#39;宝马&#39;,&#39;brandIdGeneral&#39;,&#39;idx&#39;);showAndHide(&#39;List1&#39;,&#39;hide&#39;);">宝马</li>--%>

<%-- <li onmousedown="getValue(&#39;generalBrandName&#39;,&#39;奥迪&#39;,&#39;brandIdGeneral&#39;,&#39;idx&#39;);showAndHide(&#39;List1&#39;,&#39;hide&#39;);">奥迪</li>--%>

<%-- </ul>--%>

</p>

</p>

</td>

</tr>

........省略常规脚本

<tr>

<th>汽车厂商名:</th>

<td>

<input type="text" id="brandName" name="brandName" value="${*.brandName}" style="width:180px" data-validation-engine="validate[required]" <c:if test="${!empty carType.brandId}"> disabled="disabled" </c:if> onfocus="showAndHide(&#39;List2&#39;,&#39;show&#39;);" onblur="showAndHide(&#39;List2&#39;,&#39;hide&#39;);" />

<input type="hidden" id="brandId" name="brandId" value="${*.brandId}" style="width:180px" />

<span class="required">必填*</span>

<p class="Menu" id="List2">

<p class="Menu2" id="ListLi2">

</p>

</p>

</td>

</tr>

Copy after login

3. Implement ajax asynchronous request filtering based on input content through JS

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

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

//页面加载的时候

jQuery(function($) {

if (navigator.userAgent.indexOf("MSIE") > 0) {

document.getElementById(&#39;generalBrandName&#39;).attachEvent("onPropertyChange", appendList);

document.getElementById(&#39;brandName&#39;).attachEvent("onPropertyChange", appendList);

}

else if (navigator.userAgent.indexOf("Firefox") > 0) {

document.getElementById(&#39;generalBrandName&#39;).addEventListener("input", appendList, false);

document.getElementById(&#39;brandName&#39;).addEventListener("input", appendList, false);

}

});

//////// 预加载

jQuery(function($) {

txtValue = $("#generalBrandName").val();

//////// 给txtbox绑定键盘事件

$("#generalBrandName").bind("keyup", function() {

var currentValue = $(this).val();

if (currentValue != txtValue) {

appendList(&#39;List1&#39;,currentValue);

txtValue = currentValue;

}

});

txtValue = $("#brandName").val();

//////// 给txtbox绑定键盘事件

$("#brandName").bind("keyup", function() {

var currentValue = $(this).val();

if (currentValue != txtValue) {

appendList(&#39;List2&#39;,currentValue);

txtValue = currentValue;

}

});

});

//实现动态显示下拉列表内容的function

//根据输入框中的值来筛选obj中的值

function appendList(obj,value){

value = encodeURIComponent(value); value = encodeURIComponent(value); //两次使用encodeURI()

switch(obj){

case "List1": //根据车品牌名来刷选List1中的值

$.getJSON(

ctx + "/car/carmodel/**.do",

{keyWord : value, id : new Date().getTime()}, <!-- 产生一个时间戳,不让IE以为是相同的URL而使用cache -->

function (json) {

createLis(&#39;ListLi1&#39;,json);

}

);

break;

case "List2": //根据车厂商名来刷选List2中的值

$.getJSON(

ctx + "/car/carmodel/**.do",

{keyWord : value, id : new Date().getTime()}, <!-- 产生一个时间戳,不让IE以为是相同的URL而使用cache -->

function (json) {

createLis(&#39;ListLi2&#39;,json);

}

);

break;

}

}

function createLis(obj,json){

switch(obj){

case "ListLi1": //根据车品牌名来刷选List1中的值

var executerp = document.getElementById(obj); //动态生成下拉列表框

executerp.innerHTML="";

var ul=document.createElement("ul");

$.each(json, function (i, item) {

var li=document.createElement("li");

var str = "getValue(&#39;generalBrandName&#39;,&#39;"+item.brandNameGeneral+"&#39;,&#39;brandIdGeneral&#39;,&#39;"+item.brandIdGeneral+"&#39;);showAndHide(&#39;List1&#39;,&#39;hide&#39;)";

li.setAttribute("onmousedown",str);

li.appendChild(document.createTextNode(item.brandNameGeneral));

ul.appendChild(li);

});

executerp.appendChild(ul);

break;

case "ListLi2": //根据车厂商名来刷选List2中的值

var executerp = document.getElementById(obj); //动态生成下拉列表框

executerp.innerHTML="";

var ul=document.createElement("ul");

$.each(json, function (i, item) {

var li=document.createElement("li");

var str = "getValue(&#39;brandName&#39;,&#39;"+item.brandName+"&#39;,&#39;brandId&#39;,&#39;"+item.brandId+"&#39;);showAndHide(&#39;List1&#39;,&#39;hide&#39;)";

li.setAttribute("onmousedown",str);

li.appendChild(document.createTextNode(item.brandName));

ul.appendChild(li);

});

executerp.appendChild(ul);

break;

}

}

//显示或者隐藏层

function showAndHide(obj,types){

var Layer=window.document.getElementById(obj);

switch(types){

case "show":

Layer.style.display="block";

appendList(obj,&#39;&#39;);

break;

case "hide":

Layer.style.display="none";

break;

}

}

//获取选中节点的内容

function getValue(obj1,str,obj2,idx){

var input=window.document.getElementById(obj1);

input.value=str;

var input=window.document.getElementById(obj2);

input.value=idx;

}

Copy after login

4. Display effect
Ajax implements the effect of changing the text of the input box to display the drop-down list

The above is the entire content of this article, I hope It will be helpful for everyone’s learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

JQuery uses $.ajax and checkbox to implement the next notification function

Through JS in Ajax The code automatically obtains the form element value

The above is the detailed content of Ajax implements the effect of changing the text of the input box to display the drop-down list. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to solve the 403 error encountered by jQuery AJAX request How to solve the 403 error encountered by jQuery AJAX request Feb 20, 2024 am 10:07 AM

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

How to solve jQuery AJAX request 403 error How to solve jQuery AJAX request 403 error Feb 19, 2024 pm 05:55 PM

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

How to solve the problem of jQuery AJAX error 403? How to solve the problem of jQuery AJAX error 403? Feb 23, 2024 pm 04:27 PM

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

PHP vs. Ajax: Solutions for creating dynamically loaded content PHP vs. Ajax: Solutions for creating dynamically loaded content Jun 06, 2024 pm 01:12 PM

Ajax (Asynchronous JavaScript and XML) allows adding dynamic content without reloading the page. Using PHP and Ajax, you can dynamically load a product list: HTML creates a page with a container element, and the Ajax request adds the data to that element after loading it. JavaScript uses Ajax to send a request to the server through XMLHttpRequest to obtain product data in JSON format from the server. PHP uses MySQL to query product data from the database and encode it into JSON format. JavaScript parses the JSON data and displays it in the page container. Clicking the button triggers an Ajax request to load the product list.

Asynchronous data exchange using Ajax functions Asynchronous data exchange using Ajax functions Jan 26, 2024 am 09:41 AM

How to use Ajax functions to achieve asynchronous data interaction With the development of the Internet and Web technology, data interaction between the front end and the back end has become very important. Traditional data interaction methods, such as page refresh and form submission, can no longer meet user needs. Ajax (Asynchronous JavaScript and XML) has become an important tool for asynchronous data interaction. Ajax enables the web to use JavaScript and the XMLHttpRequest object

Understanding Ajax Frameworks: Explore Five Common Frameworks Understanding Ajax Frameworks: Explore Five Common Frameworks Jan 26, 2024 am 09:28 AM

Understanding the Ajax Framework: Explore five common frameworks, requiring specific code examples Introduction: Ajax is one of the essential technologies in modern web application development. It has become an indispensable part of front-end development because of its features such as supporting asynchronous data interaction and improving user experience. In order to better understand and master the Ajax framework, this article will introduce five common Ajax frameworks and provide specific code examples to help readers gain an in-depth understanding of the usage and advantages of these frameworks. 1. jQuery jQuery is currently the most

PHP and Ajax: Ways to Improve Ajax Security PHP and Ajax: Ways to Improve Ajax Security Jun 01, 2024 am 09:34 AM

In order to improve Ajax security, there are several methods: CSRF protection: generate a token and send it to the client, add it to the server side in the request for verification. XSS protection: Use htmlspecialchars() to filter input to prevent malicious script injection. Content-Security-Policy header: Restrict the loading of malicious resources and specify the sources from which scripts and style sheets are allowed to be loaded. Validate server-side input: Validate input received from Ajax requests to prevent attackers from exploiting input vulnerabilities. Use secure Ajax libraries: Take advantage of automatic CSRF protection modules provided by libraries such as jQuery.

See all articles