国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 javascript+PHP 應(yīng)用一:網(wǎng)頁制作中雙下拉菜單的動態(tài)實現(xiàn)(轉(zhuǎn)貼)

javascript+PHP 應(yīng)用一:網(wǎng)頁制作中雙下拉菜單的動態(tài)實現(xiàn)(轉(zhuǎn)貼)

Jun 02, 2017 am 11:29 AM
document javascript menu quot

菜單|動態(tài)|網(wǎng)頁|下拉

JavaScript+PHP 應(yīng)用一:網(wǎng)頁制作中雙下拉菜單的動態(tài)實現(xiàn)

---摘自互聯(lián)網(wǎng)

在網(wǎng)頁制作中,常常遇到這種情況,通過主下拉菜單的選擇,動態(tài)的生成子下拉菜單。例如:在主菜單中有“焦點新聞”、“生活時尚”、“心情故事”三個選項,通過“焦點新聞”的選擇,子菜單自動生成如“國內(nèi)”、“國際”、“體育”、“文娛”,依此類推。

利用javascript,我們可以輕松實現(xiàn)上述效果。但問題是,如果菜單中的選項是從數(shù)據(jù)庫(或其他文件)中動態(tài)提取,實現(xiàn)起來就并不是輕而易舉的了。筆者根據(jù)自己的實際經(jīng)驗,向大家介紹一種利用javascript + php的實現(xiàn)方法,文中的數(shù)據(jù)庫采用mysql。在本例中,筆者還將介紹在每一次表單提交之后,如何返回上一次菜單選項的選擇狀態(tài)。

文章中所介紹的php的作用,一是用來從數(shù)據(jù)庫中提取菜單選項,另一作用,就是用來生成javascript代碼。讀者可以采用自己熟悉的解釋型語言,如asp。

為了簡化代碼,筆者假設(shè)主菜單已經(jīng)通過html構(gòu)造,由于子菜單需要動態(tài)設(shè)計,所以只繪制了基本框架,html代碼如下:





我們需要考慮的是,菜單的onchange()事件需要完成哪些步驟。其大致過程是,根據(jù)主菜單的選項,構(gòu)造子菜單項目。而子菜單的項目文字最好事先設(shè)定。根據(jù)這個思路,筆者采用了javascript中的聯(lián)合數(shù)組記錄子菜單選項,并由php在加載時自動生成。由此,筆者設(shè)計了如下的javascript函數(shù)setmenu():

function setmenu(){
menu=array("a","b","c"); //構(gòu)造menu聯(lián)合數(shù)組
<?php //開始php程序
$db = new my_db();
$db->database = "***"; //構(gòu)造新的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 =&#39;".$id."&#39;");
//假設(shè)菜單選項存放在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聯(lián)合數(shù)組的填充
unset($smenu); //刪除smenu變量
}
}
?> //結(jié)束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語言,當單擊主菜單時,子菜單將自動更新。同樣道理,讀者可以根據(jù)此思路,創(chuàng)造更復(fù)雜的多重菜單選項。

最后,筆者簡要介紹一下,如何實現(xiàn)在表單提交后,仍然保持菜單項上一次的狀態(tài)。技巧其實很多,而筆者采用的是隱含變量法。在表單中添加如下代碼:





我們只需要在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;
?>

至此,我們已經(jīng)實現(xiàn)雙下拉菜單的動態(tài)實現(xiàn)方法。
??????????????? ?







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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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)

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

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 implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

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.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

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 and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

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

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

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.

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

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

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

How to solve the problem that document.cookie cannot be obtained How to solve the problem that document.cookie cannot be obtained Nov 23, 2023 am 10:02 AM

Solutions for document.cookie not being obtained: 1. Browser privacy settings; 2. Same-origin policy; 3. HTTPOnly Cookie; 4. JavaScript code error; 5. Cookie does not exist or expires; 6. Cross-domain issues; 7. Viewer mode; 8. Server problems; 9. JavaScript execution timing; 10. Check console log, etc.

See all articles