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>