javascript+PHP 应用一:网页制作中双下拉菜单的动态实现(转贴)
菜单|动态|网页|下拉
JavaScript+PHP 应用一:网页制作中双下拉菜单的动态实现
---摘自互联网
在网页制作中,常常遇到这种情况,通过主下拉菜单的选择,动态的生成子下拉菜单。例如:在主菜单中有“焦点新闻”、“生活时尚”、“心情故事”三个选项,通过“焦点新闻”的选择,子菜单自动生成如“国内”、“国际”、“体育”、“文娱”,依此类推。
利用javascript,我们可以轻松实现上述效果。但问题是,如果菜单中的选项是从数据库(或其他文件)中动态提取,实现起来就并不是轻而易举的了。笔者根据自己的实际经验,向大家介绍一种利用javascript + php的实现方法,文中的数据库采用mysql。在本例中,笔者还将介绍在每一次表单提交之后,如何返回上一次菜单选项的选择状态。
文章中所介绍的php的作用,一是用来从数据库中提取菜单选项,另一作用,就是用来生成javascript代码。读者可以采用自己熟悉的解释型语言,如asp。
为了简化代码,笔者假设主菜单已经通过html构造,由于子菜单需要动态设计,所以只绘制了基本框架,html代码如下:
我们需要考虑的是,菜单的onchange()事件需要完成哪些步骤。其大致过程是,根据主菜单的选项,构造子菜单项目。而子菜单的项目文字最好事先设定。根据这个思路,笔者采用了javascript中的联合数组记录子菜单选项,并由php在加载时自动生成。由此,笔者设计了如下的javascript函数setmenu():
function setmenu(){ menu=array("a","b","c"); //构造menu联合数组 <?php //开始php程序 $db = new my_db(); $db->database = "***"; //构造新的mysql连接,这里使用了phplib $mmenu = array("a","b","c"); //这里笔者作了简化 for ($i=0;$i<count($mmenu);$i++){ $id = $mmenu[$i]; $db->query("select menu from class where menuid ='".$id."'"); //假设菜单选项存放在class表的menu字段,menuid用来标识menu while ($db->next_record()){ $smenu[] = """.$db->f("menu")."""; } if (isset($smenu) && is_array($smenu)){ $str = implode(",",$smenu); echo "menu["$id"] =array($str);ntt"; //完成menu联合数组的填充 unset($smenu); //删除smenu变量 } } ?> //结束php程序 with (document) { id=all("mmenu").value; //获得主菜单的value值 arr_menu=menu[id]; for(i=all("smenu").options.length;i>=0;i--){ all("smenu").options.remove(i); //需要清除原有的项目 } if (arr_menu.length==0){ return; } for(i=0;i<arr_menu.length;i++){ obj=createelement("option"); obj.text=arr_class[i]; all("smenu").options.add(obj); } } }
这样每次显示文档时,php部分将解释为javascript语言,当单击主菜单时,子菜单将自动更新。同样道理,读者可以根据此思路,创造更复杂的多重菜单选项。
最后,笔者简要介绍一下,如何实现在表单提交后,仍然保持菜单项上一次的状态。技巧其实很多,而笔者采用的是隐含变量法。在表单中添加如下代码:
我们只需要在form表单的onsubmit()事件中给每个隐含变量赋值即可。即:
document.all("h1").value=document.all("mmenu").selectedindex;
document.all("h2").value=document.all("smenu").selectedindex;
为了利用隐含变量,在文档的body的onload()事件中,我们利用php方法(也可用其它方法)来控制菜单的显示:
<?php if (!isset($h1)){ //只需要判断$h1 $h1 = 0; $h2 = 0; } echo "document.all("mmenu").selectedindex=".$h1.";ntt"; echo "document.all("mmenu").click();ntt"; echo "document.all("mmenu").selectedindex=".$h1.";ntt"; echo "document.all("smenu").selectedindex=".$h2; ?>
至此,我们已经实现双下拉菜单的动态实现方法。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

On September 3, version 1.5 of the Grand Theft Auto VI document which provides a detailed overview of everything that is known about the game to date. The updated version of the document was announced in a trailer published on X (formerly Twitter).??

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
