<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="jquery-1.8.0.min.js" type="text/javascript"></script>
<script type="text/javascript">
var test = {
getValue: function (key) {
if (key == '1') {
return 'A';
} else {
return 'B';
}
},
getAjaxValue: function (key) {
$.ajax({
type: "post", async: false, cache: false, dataType: "text",
url: '../../Handler/JavaScriptSession.ashx',
data: [
{ name: "action", value: "read" },
{ name: "name", value: key },
{ name: "value", value: "" }
],
success: function (result) {
if (result != "error" || result != "true") {
alert("内部"+result);//这值能得到,并成功alert出
return "返回值"+result;
}
},
error: function () {
alert("错误");
}
});
}
}
</script>
</head>
<body>
<div >
<a onclick="alert(test.getValue('1'));">测试1</a>
<br />
<a onclick="alert(test.getAjaxValue('1'));">测试2</a>
</div>
</body>
</html>为什么,测试1 我能得到return的值,测试2我确无法得到?Ajax的async我已经设置为false了呀?
解决方案 »
- 获取body下的文本节点,两种方法有何不同?
- 页面提交客户端验证和服务端验证
- 如何给漂浮的广告加一个关闭按钮,如何写js脚本
- 关于<input type='file'>的问题
- JQuery 入门问题
- 定义对象带返回值的方法疑问.
- **请问:怎样控制文本框只能输入数字或者小数,并且小数位数不能超过2位??谢谢!急!!有相应正则表达式吗?
- 一个很cool的菜单效果,谁能把源码提取出来?
- 想js入门请各位介绍两本书,越全越好,越厚越好!
- 在一个框架页中,如何在一个frame中将另一个frame中的页面进行刷新?
- myeclipse安装aptana插件之后遇到的问题——Aptana使用问题
- jquery scroll()怎么区分横向纵向滚动条
{ name: "action", value: "read" },
{ name: "name", value: key },
{ name: "value", value: "" }
],data这样传不行吧
data (Object,String) : 发送到服务器的数据。将自动转换为请求字符串格式。GET 请求中将附加在 URL 后。查看 processData 选项说明以禁止此自动转换。必须为 Key/Value 格式。如果为数组,jQuery 将自动为不同值对应同一个名称。如 {foo:["bar1", "bar2"]} 转换为 '&foo=bar1&foo=bar2'。
跟踪发现ajax成功执行。success中得到了result的值。
alert("内部"+result);也显示了。
但test.getAjaxValue('1')得不到return的值。
var test = {
getValue : function(key) {
if (key == '1') {
return 'A';
} else {
return 'B';
}
},
getAjaxValue : function(key) {
var _result;
$.ajax({
type : "post",
async : false,
cache : false,
dataType : "text",
url : 'index.php',
data : {},
success : function(result) {
if (result != "error" || result != "true") {
alert("内部" + result);//这值能得到,并成功alert出
_result = "返回值" + result;
}
},
error : function() {
alert("错误");
}
});
return _result;
}
}
getValue: function (key) {
if (key == '1') {
return 'A';
} else {
return 'B';
}
},
getAjaxValue: function (key) {
var s = false; ////////
$.ajax({
type: "post", async: false, cache: false, dataType: "text",
url: '../../Handler/JavaScriptSession.ashx',
data: [
{ name: "action", value: "read" },
{ name: "name", value: key },
{ name: "value", value: "" }
],
success: function (result) {
if (result != "error" || result != "true") {
alert("内部" + result); //这值能得到,并成功alert出
s = "返回值" + result; ///////////
}
},
error: function () {
alert("错误");
}
});
return s;//////
}
}