$.ajax()方法进行网页间传值示例

515 查看

ajaxtext1.jsp
复制代码 代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<script type="text/javascript" language="javascript" src="jquery.js"></script> <!-- 必须包含 jquery.js文件,否则将无法传值-->
<head>
<script type="text/javascript">
function fun(){
var ss;
var radio1=document.getElementsByName("radio1"); //读取单选按钮的值
for(var i=0;i<radio1.length;i++){
if(radio1.item(i).checked){
ss=radio1.item(i).value;
break;
}
}
// var sendstr="sendstr1="+form1.text1.value+"&sendstr2="+ss;
$.ajax({
type:"POST",
url: "ajaxtest2.jsp",
async:false,
// data:sendstr, //也可以用注释的这方式来进行传值操作
data:{sendstr1:form1.text1.value,sendstr2:ss}, //也可以用 $("#text1").val()的方式读取text1中的元素值
success:function(data){
alert(data);
}
});
}
</script>
</head>
<body>
<div align="center">
<form id="form1" name="form1" action="" method="post" >
输入:<input type="text" name="text1" id="text1"><br>
金球:<input type="radio" name="radio1" value="梅西">梅西
<input type="radio" name="radio1" value="C罗">C罗<br>
<input type="button" value="提交" onclick="fun()">
</form>
</div>
</body>
</html>

ajaxtest2.jsp
复制代码 代码如下:

<pre code_snippet_id="269881" snippet_file_name="blog_20140402_4_8173458" class="javascript" name="code"><%@ page contentType="text/html;charset=utf-8"%>

<%
request.setCharacterEncoding("utf-8");
String sendstr1=request.getParameter("sendstr1");
String sendstr2=request.getParameter("sendstr2");
String sReturn=sendstr1+"_new_"+sendstr2;
%>
<%=sReturn%></pre><pre code_snippet_id="269881" snippet_file_name="blog_20140402_5_3124712" class="javascript" name="code"></pre><pre code_snippet_id="269881" snippet_file_name="blog_20140402_5_3124712" class="javascript" name="code"><h2 class="titName SG_txta" id="t_4f925fc30100la36">JQuery中$.ajax()方法参数详解 问百度或Google
</h2></pre>
<pre></pre>
<div style="top:177px"></div>
<pre></pre>