JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Servlet/JSP 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Re:[問題]即時多層下拉選單 [Re:bernardvai]
catte





發文: 34
積分: 0
於 2011-06-28 17:54 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
bernardvai感謝你 已經用好了^ ^
而那個城市名後會有多一個空格,那是我無異的~~
那個bernardvai我想再問你一個問題
就是如果我第二個下拉式選擇"好記"之後會在這個網頁的下面出現好記的菜單(有表格) 要怎麼做
下面這個是我"好記菜單的資料表(Table_1)"


reply to postreply to post
作者 Re:[問題]即時多層下拉選單 [Re:catte]
bernardvai





發文: 491
積分: 0
於 2011-06-29 09:58 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
如你是明白這個程式的運作原理, 用類似的方法造出第三個下拉式選單, 其內容會按第二個選單而變並不難.
如不是造出第三個下拉式選單, 用 TextArea, 或純靜態文字也可以.
用document.getElementById("menu").innerHTML= menu;

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
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>Test</title>
</head>
 
<body>
<SCRIPT LANGUAGE="javascript">
function ChangeSelect(city, region){
  val = city.options[city.options.selectedIndex].value;
  if (val == 'a'){
     region.length=2;
     region.options[0].value= 'x';
     region.options[0].text=  'x';
     region.options[1].value= 'y';
     region.options[1].text=  'y';
  }
  if (val == 'b'){
     region.length=3;
     region.options[0].value= 't';
     region.options[0].text=  't';
     region.options[1].value= 'u';
     region.options[1].text=  'u';
     region.options[2].value= 'v';
     region.options[2].text=  'v';   
  }
  document.getElementById("menu").innerHTML="";
}
 
function ChangeRegion(region){
  val = region.options[region.options.selectedIndex].value; 
  var menu="";
  if (val == 'x') menu= '<table border=1><tr><td>name</td><td>money</td><td>calorie</td></tr>'+
                  '<tr><td>chicken rice</td><td>100</td><td>10</td></tr>'+
                  '<tr><td>noodle</td><td>50</td><td>15</td></tr>'+
                  '</table>';
  if (val == 'y') menu= '<table border=1><tr><td>name</td><td>money</td><td>calorie</td></tr>'+
                  '<tr><td>apple juice</td><td>100</td><td>10</td></tr>'+
                  '<tr><td>cola</td><td>50</td><td>15</td></tr>'+
                  '</table>';
                  
  if (val == 't') menu= '<table border=1><tr><td>name</td><td>money</td><td>calorie</td></tr>'+
                  '<tr><td>t1</td><td>100</td><td>10</td></tr>'+
                  '<tr><td>t2</td><td>50</td><td>15</td></tr>'+
                  '</table>';
 
  if (val == 'u') menu= '<table border=1><tr><td>name</td><td>money</td><td>calorie</td></tr>'+
                  '<tr><td>u1</td><td>100</td><td>10</td></tr>'+
                  '<tr><td>u2</td><td>50</td><td>15</td></tr>'+
                  '</table>';
                  
  if (val == 'v') menu= '<table border=1><tr><td>name</td><td>money</td><td>calorie</td></tr>'+
                  '<tr><td>v1</td><td>100</td><td>10</td></tr>'+
                  '<tr><td>v2</td><td>50</td><td>15</td></tr>'+
                  '</table>';
                  
  document.getElementById("menu").innerHTML=menu;                          
  
}
 
</script>
 
<form name="selection">
<select name=city onChange="ChangeSelect(document.selection.city,document.selection.region)" size="1">
<option>==請選擇城市==</option>
<option value="a">a</option>
<option value="b">b</option>
</select><BR>
選擇您要的區域</font><br>
<SELECT name=region onChange="ChangeRegion(document.selection.region)" >
 
</SELECT> </p>
</form> 
<hr>
<span id=menu>
</span>


bernardvai edited on 2011-06-29 10:25
reply to postreply to post
作者 Re:[問題]即時多層下拉選單 [Re:bernardvai]
catte





發文: 34
積分: 0
於 2011-06-29 21:58 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
bernardvai問一下 你那個資料表是不是跟上一個資料表(city)一樣的寫阿!!

reply to postreply to post
作者 Re:[問題]即時多層下拉選單 [Re:catte]
bernardvai





發文: 491
積分: 0
於 2011-06-30 14:40 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
假設有另一個 table 名為 menu
1
2
3
4
5
6
7
aa       food(name)  money     calorie
===      ==========  =====     =======
麥當勞   漢堡飽       100       50
麥當勞   魚柳飽       120       60
肯德基   炸雞         115       55
...
... 


reply to postreply to post
作者 Re:[問題]即時多層下拉選單 [Re:bernardvai]
catte





發文: 34
積分: 0
於 2011-07-13 10:41 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
bernardvai 我想問一下這個的JSP要怎麼寫

menu= '<table border=1><tr><td>name</td><td>money</td><td>calorie</td></tr>'+
'<tr><td>v1</td><td>100</td><td>10</td></tr>'+
'<tr><td>v2</td><td>50</td><td>15</td></tr>'+
'</table>';


reply to postreply to post
作者 Re:[問題]即時多層下拉選單 [Re:catte]
bernardvai





發文: 491
積分: 0
於 2011-07-13 11:34 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
catte wrote:
bernardvai 我想問一下這個的JSP要怎麼寫

menu= '<table border=1><tr><td>name</td><td>money</td><td>calorie</td></tr>'+
'<tr><td>v1</td><td>100</td><td>10</td></tr>'+
'<tr><td>v2</td><td>50</td><td>15</td></tr>'+
'</table>';

我本身並不喜歡 JSP 將 html code 和 java code 混合使用.
你問到這個, 我也只好大約寫這個出來
1
2
3
4
5
6
7
menu= '<table border=1><tr><td>name</td><td>money</td><td>calorie</td></tr>'+
 
<% rs3.first(); do{%>
     '<tr><td><%=rs3.getString("name")%></td><td><%=rs3.getString("money")%></td><td><%=rs3.getString("calorie")%></td></tr>'+
<% }while(rs3.next());%>
 
'</table>';


bernardvai edited on 2011-07-17 01:42
reply to postreply to post
作者 Re:[問題]即時多層下拉選單 [Re:bernardvai]
catte





發文: 34
積分: 0
於 2011-08-15 09:35 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
抱歉bernardvai 我有一個問題,你是把
String SQL2 = " select * from city where id='" + rs.getString("id") + "'";
ResultSet rs2 = stmt2.executeQuery(SQL2);放在哪??
我一直出現rs2的錯誤,我把SQL2放了很多位置都不行= =
程式:
int i=0;
Statement stmt=Conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
Statement stmt2=Conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
Statement stmt3=Conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String SQL = "Select id From a";
ResultSet rs = stmt.executeQuery(SQL);
%>

<SCRIPT LANGUAGE="javascript">
<!--
function ChangeSelect(city, region)
{
val = city.options[city.options.selectedIndex].value;

<%while(rs.next()){

String SQL2 = " select * from city where id='" + rs.getString("id") + "'";
ResultSet rs2 = stmt2.executeQuery(SQL2);

%>
if (val == '<%=rs.getString("id")%>')
{

<%while(rs2.next()){
i++;
}%>
region.length = <%=i%>;
<%
i=0;
rs2.first();
do{%>
region.options[<%=i%>].value = '<%=rs2.getString("aa")%>';
region.options[<%=i%>].text = '<%=rs2.getString("aa")%>';
<%
i++;
}while(rs2.next());
i=0;%>

}
<%}
rs.first();%>
document.getElementById("menu").innerHTML="";
}
function ChangeSelect(city, region)
{
val = region.options[region.options.selectedIndex].value;
var menu="";

<%while(rs2.next()){

  
String SQL3 = " select * from menu where aa='" + rs2.getString("aa") + "'";
ResultSet rs3 = stmt3.executeQuery(SQL3);

%>
if (val == '<%=rs2.getString("aa")%>')
{

menu= '<table border=1><tr><td>name</td><td>money</td><td>calorie</td></tr>';

<% rs3.first(); do{%>
'<tr><td><%=rs3.getString("name")%></td><td><%=rs3.getString("money")%></td><td><%=rs3.getString("calorie")%></td></tr>';
<% }while(rs3.next());%>

'</table>';


}
<%}
rs2.first();%>
document.getElementById("menu").innerHTML=menu;
}
錯誤:
org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 74 in the jsp file: /001.jsp
rs2 cannot be resolved
71: val = region.options[region.options.selectedIndex].value;
72: var menu="";
73:
74: <%while(rs2.next()){
75:
76:   
77: String SQL3 = " select * from menu where aa='" + rs2.getString("aa") + "'";

An error occurred at line: 77 in the jsp file: /001.jsp
rs2 cannot be resolved
74: <%while(rs2.next()){
75:
76:   
77: String SQL3 = " select * from menu where aa='" + rs2.getString("aa") + "'";
78: ResultSet rs3 = stmt3.executeQuery(SQL3);
79:
80: %>

An error occurred at line: 81 in the jsp file: /001.jsp
rs2 cannot be resolved
78: ResultSet rs3 = stmt3.executeQuery(SQL3);
79:
80: %>
81: if (val == '<%=rs2.getString("aa")%>')
82: {
83:
84: menu= '<table border=1><tr><td>name</td><td>money</td><td>calorie</td></tr>';

An error occurred at line: 96 in the jsp file: /001.jsp
rs2 cannot be resolved
93:
94: }
95: <%}
96: rs2.first();%>
97: document.getElementById("menu").innerHTML=menu;
98: }
99: -->

Stacktrace:
  org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:95)
  org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
  org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:457)
  org.apache.jasper.compiler.Compiler.compile(Compiler.java:374)
  org.apache.jasper.compiler.Compiler.compile(Compiler.java:352)
  org.apache.jasper.compiler.Compiler.compile(Compiler.java:339)
  org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:594)
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:344)
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
  org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


reply to postreply to post
作者 Re:[問題]即時多層下拉選單 [Re:ㄚ孝]
jk47





發文: 159
積分: 0
於 2011-08-15 11:19 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
流程:
1.首先 從DB抓出第一層的國家選定後,用AJAX將值往後送
2.根據傳的值從DB撈出,用JAVA組成HTML語法的下拉式選項(包含ONCHANGE())
3.前端用INNERHTML,接AJAX的回傳值
4.選定第二層,將選定值用AJAX將值往後送
5.根據傳的值從DB撈出,用JAVA組成HTML語法的下拉式選項
6.前端用INNERHTML,接AJAX的回傳值
結束這回合

基本上,若樓主技術上許可,1-3和4-6 AJAX後送的JAVA CODE 應可為同一個,唯需注意所下SQL與是否要包含SELECT的動作選項,即ONCHANGE()等


reply to postreply to post
喝著濃濃的JAVA,看著MYSQL耀出水面
旁邊NetBeans抓著GlassFish與DUKE在嬉鬧
現在手上又拿著ORACLE在研究
(ORACLE=甲骨文=契文)
go to first page go to previous page  1   2   3  go to next page go to last page
» JWorld@TW »  Servlet/JSP 討論區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8