$.ajax()方法进行网页间传值示例
程序员文章站
2023-11-13 17:22:04
ajaxtext1.jsp 复制代码 代码如下: <%@ page language="java" import="java.util.*" pageencoding...
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>
复制代码 代码如下:
<%@ 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>