作者TonyQ (骨头)
看板Ajax
标题Re: [问题] 有关在asp中使用javacript变数的问题
时间Mon May 26 22:14:27 2008
※ 引述《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