87,914
社区成员
发帖
与我相关
我的任务
分享
<title>aaaa页面往bbbb页面通过jquery的AJAX传递参数</title>
<base target="_blank" /> <!-- 确保超链接都是在新窗口打开 -->
<script src="http://www.w3school.com.cn/jquery/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(function () {
$('.div11 a').click(function () {
//先获取包含有汉字的href=newurl
var newurl = $(this).attr("href") + "&hanzi=" +encodeURIComponent( $(this).text());//注意编码
//////这里要先跳转到cccc.asp?act=find&name=333&age=25&hanzi=可获取汉字的超链接11111(也要带这些汉字)
/////然后再开始构建向bbbb.asp传递参数的ajax
newurl = newurl.replace('cccc.asp', 'bbbb.asp')///替换下页面就行了,注意bbbb.asp中用request或request.querystring获取参数,因为是参数get提交
alert(newurl)
$.ajax({
url: newurl,//////////////////////////
type: 'POST',
data: {},//
success: function (data) { alert('成功:' + data) },
error: function (xhr) { alert('服务器出错,返回内容:' + xhr.responseText) }
});
//return false;//加上这个就会阻止向cccc.asp跳转,但是我是希望既向cccc.asp跳转,同时又可以通过ajax向bbbb.asp传递参数,两个页面的参数一样,都在原链接的基础上带上hanzi=可获取汉字的超链接1111111。
});
});
</script>
<div class="div11">
<li><a href="cccc.asp?act=find&name=333&age=25">可获取汉字的超链接1111111</a></li>
<li><a href="cccc.asp?act=find&name=444&age=60">可获取汉字的超链接2222222</a></li>
<li><a href="cccc.asp?act=find&name=666&age=40">可获取汉字的超链接3333333</a></li>
</div>
$(function () {
$('.div11 a').click(function(){
//先获取包含有汉字的href=newurl
var newurl = $(this).attr("href") + "&hanzi=" +encodeURIComponent( $(this).text());//注意编码
var _oldurl = newurl;
//////这里要先跳转到cccc.asp?act=find&name=333&age=25&hanzi=可获取汉字的超链接11111(也要带这些汉字)
/////然后再开始构建向bbbb.asp传递参数的ajax
newurl = newurl.replace('cccc.asp', 'bbbb.asp')///替换下页面就行了,注意bbbb.asp中用request或request.querystring获取参数,因为是参数get提交
alert(newurl)
$.ajax({
url: newurl,//////////////////////////
type: 'POST',
data: {},//
success: function (data) {
alert('成功:' + data)
location.href = _oldurl; //这里跳转,成功后跳转,如果要ajax失败也跳转那就在下面的error里面也加上这
},
error: function (xhr) { alert('服务器出错,返回内容:' + xhr.responseText) }
});
//return false;//加上这个就会阻止向cccc.asp跳转,但是我是希望既向cccc.asp跳转,同时又可以通过ajax向bbbb.asp传递参数,两个页面的参数一样,都在原链接的基础上带上hanzi=可获取汉字的超链接1111111。
});
});
$(function () {
$('.div11 a').click(function () {
var newurl = $(this).attr("href") + "&hanzi=" +encodeURIComponent($.trim($(this).text()));//注意编码
var oldurl = newurl; // 保存向bbbb.asp跳转的地址
newurl = newurl.replace('cccc.asp', 'bbbb.asp');
$.ajax({
url: newurl,
type: 'POST',
data: {},//
success: function (data) { alert('成功:' + data) },
error: function (xhr) { alert('服务器出错,返回内容:' + xhr.responseText) }
});
window.open(oldurl); // 直接通过window.open打开,。
return false;
});
});
方式二:
重设href属性
$(function () {
$('.div11 a').click(function () {
var newurl = '' , oldurl = '';
if($(this).attr('oldurl')){ // 如果有oldurl属性,则不再进行url拼接,直接获取href属性
newurl = oldurl = $(this).attr("href"); //
}else{
newurl = $(this).attr("href") + "&hanzi=" +encodeURIComponent($.trim($(this).text()));
oldurl = newurl;
$(this).attr('oldurl',$(this).attr("href")); // 记录原链接地址
$(this).attr('href',oldurl); // 重设href属性
}
newurl = newurl.replace('cccc.asp', 'bbbb.asp');
$.ajax({
url: newurl,
type: 'POST',
data: {},
success: function (data) { alert('成功:' + data) },
error: function (xhr) { alert('服务器出错,返回内容:' + xhr.responseText) }
});
});
});