当前位置:编程学习 > JSP >>

jsp中onclick的问题。

我要做一个购买商品的页面,需要点击图片“确认并提交”来触发一个方法(该方法为从页面上去的需要的数据保存到数据库中),问下怎么写这个onlick事件,怎么从该页面取值传到方法的sql语句中。(页面主要去商品名称,单价,数量,总价等)
补充:希望给出代码
追问:问下<input type="text" name="aa">中“text”中存得是什么?我要传得值?<script type="text/javascript">
    function fun()
    { var price = document.getElementById('price').value;
      var count = document.getElementById('count').value;
      document.getElementById('total').value = count*price;
      document.getElementById('total1').value = count*price;  }
  </script>
我现在只想获得其中用户输入的数量conut的值,带着他<a herf="mmm?count=..."跳转,这个怎么取我知道这种做法,现在的问题是我在解决:用户输入商品数量,页面自动计算总价的时候用了js

那么我怎么得到js中总价total的值呢?将这个值用做跳转所携带的参数传到下一页面?
document.getElementById('total1').value = count*price;问一下,这是我写的JS代码
 <script type="text/javascript">
    function fun()
    { var price = document.getElementById('price').value;
      var count = document.getElementById('count').value;
      document.getElementById('total').value = count*price;
      document.getElementById('total1').value = count*price;  }
  </script>
现在我只想获得其中count的值,带它<a herf="地址?count" >跳转<script type="text/javascript">
    function fun()
    { var price = document.getElementById('price').value;
      var count = document.getElementById('count').value;
      document.getElementById('total').value = count*price;
      document.getElementById('total1').value = count*price;  }
  </script>
我现在只想获得其中用户输入的数量conut的值,带着他<a herf="mmm?count=..."跳转,这个怎么取
答案:发送页面大体如下:
<form name="form1" method="post" action="">
商品名称:<input type="text" name="aa"><br>
单价:<input type="text" name="bb"><br>
数量:<input type="text" name="cc"><br>
总价:<input type="text" name="dd"><br>
</form>
<img src="http://img.zhaoxi.net/img/logo-zhidao.gif" title="点击提交" onclick="if(confirm('确认提交?')){document.form1.submit();}" border="1">

接收页面直接接收:
<%
String aa=Request.getParameter("aa"); 
String bb=Request.getParameter("bb"); 
String cc=Request.getParameter("cc"); 
String dd=Request.getParameter("dd"); 
%>
其他:直接就让这个表单提交不用onclick事件 
转到一个写好的JSp页面中,这个页中只有JSP编码
例如:
<%
这个用Request得到传过来的页面中文本框name="name" 的值然后用一个字符串变量接受
  String name=Request.getParameter("name"); 
以此类推 把所有的数据都得到 然后调用存储方法把数据存到数据库中就行了。
%>

希望对你有所帮助 jsp页面:
<Input class=cssButton name="InsureButton" type=button value="确定"  onclick="InsureClick()">
js页面:
function InsureClick()
{
String strSql="select * from tablename where price='"+fm.price.value+"'";//单价就传过来了
}
点击jsp页面的确定按钮会调用js页面的 InsureClick()方法,从jsp页面传值到js页面 写一个form表单,提交到一个servlet或者action中,表单中添加几个
<input type="text" name="xxx"/>,再来一个提交按纽。
xxx就是商品名,单价等等的标记,而不是他们的值,值是你添在这个text中的内容
当点击提交的时候,就是向后台传值了
你用servlet的话,可以使用String yyy=request.getParameter("xxx");来获取前台传过来的值,这里的xxx就是前台JSP中name的xxx的值。这个值就是yyy
然后,写SQL语句吧
如果是struts,可以使用formBean,不需要用request获取,可以使用
类名 对象 =(类名)form;来或者这个对象,可以直接存这个对象。
或者和servlet一样,一个一个获取值
这个如果不懂,可以不管,总之我的方法能实现你的取值。 
不懂的话,可以继续追问我!~ 
我了个去的,都不追问我
js中如果或者这个总数,那是不能够再传这个form表单的值过去的,因为js中你要定义你个URL
URL+求到的总数,用js打开这个页面,那这个就是get提交了,你就需要把所有的form里的值都一个一个加到url中,比如
你求得了total,并且取得了productName什么的
var url="地址?total="+total+"&productName="+productName+.......;
然后你需要open这个url
你可以在后台编写求得total这样就不用这么麻烦写JS了 
哎,我给你写代码吧, 不能对不起我写的这么多字,稍等!~ 
<script type="text/javascript">
	function submit(){
		var productName = document.getElementById("productName").value;
		var price = document.getElementById("price").value;
		var count = document.getElementById("count").value;
		var total = price*count;
		var url="地址?total="+total+"&productName="+productName;
		open(url,'_self');
	}
</script>
<body>
<form>
<input type="text" name="productName"/>
<input type="text" name="price"/>
<input type="text" name="count"/>
<input type="submit" name="submit" onclick="submit()"/>
</form>
</body> 你保存的数据数据库中本来就有部分一样,因为你这个商品是从数据库中读取到的,所以只要知道商品的id,所以你知道知道商品的数量就可以了,用onclick('传值')括号里面是你传的值。利用ajax就可以提交了 
<script>
var xmlHttpRequest;
	function createXmlHttpRequest(){
		if(window.ActiveXObject){
			return new ActiveXObject("Microsoft.XMLHTTP");
		}else{
			return new XMLHttpRequest();
		}
	}
function doRefresh()
	{
//从这里可以用document.getElementById()获取你需要的信息
var url="<%=request.getContextPath() %>/NumberServlet?shangp=
			xmlHttpRequest=createXmlHttpRequest();
			xmlHttpRequest.onreadystatechange=exculet;
			xmlHttpRequest.open("post",url,true);
			xmlHttpRequest.send(null);
	}
function exculet(){
		if(xmlHttpRequest.readyState==4){
    		if(xmlHttpRequest.status==200){  
    			var score=xmlHttpRequest.responseText;
                                               //接收返回值,可以判断是否添加成功
                                }
                              } 
}
<script>
<img src="http://img.zhaoxi.net/img/logo-zhidao.gif" title="点击提交" onclick="doRefresh('商品id')" border="0">

上一个:COM,JScript,JSP有什么不同
下一个:如何在jsp中放入文本框,并且要获取文本框中输入的值。

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,