Re: [問題] 有關在asp中使用javacript變數的問題
※ 引述《lemarulie (另一個我)》之銘言:
: 請問一下各位大大
: 我有個問題已經想了好久可是都沒相出好的方法來解決
: 希望大大指點一下
: 我已在asp的網頁中連了連線連到資料庫
: 並從資料庫抓了想個值回來
: 這兩個值我存在a和b這兩個變數中
: 現在我想要在按下按鈕後
: 使用javascript的alert方式
: 提醒使用者這兩個值是多少
: 可是一直想不出什麼方法能把asp中的變數傳給javascript
: 能否請各位大大指點我一下?
是ASP還是ASP.net?
原理其實差不多
基本上必須在頁面載入時 就完成資料的計算
然後比方說一個html長這樣
<html>
<head>
<title>測試用html </title>
</head>
<script type="text/javascript">
var a="<%=a%>"; //此時伺服器的a被用來產生一個js變數
var b="<%=b%>"; //同上
</script>
<body>
<input type="button" onclick="alert(a);" value="showA"/>
<input type="button" onclick="alert(b);" value="showB"/>
</body>
</html>
────────────────────────────────
這是以asp的概念來想啦, .net的話其實差不多,
可以把它指定給一個隱藏的hidden input,
再用js去load那個hidden input的內容來alert。
────────────────────────────────
不過寫js跟server side的溝通一定要意識到一點,
不管server side幫你做什麼事情,最後就是會變成html,
而js的工作是從變成html才會開始執行,
所以所謂的asp變數傳給js 是一種上對下的關係,而不是平行的關係。
--
I am a person, and I am always thinking .
Thinking in love , Thinking in life ,
Thinking in why , Thinking in worth.
I can't believe any of what ,
I am just thinking then thinking ,
but worst of all , most of mine is thinking not actioning...
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.134.27.68
討論串 (同標題文章)
Ajax 近期熱門文章
PTT數位生活區 即時熱門文章