jquery done()不能获取外部变量
16lz
2021-01-22
$('#delete').on('click', function(e){
var id = '123';
// layer.confirm('confirm delete?', {
// btn: ['ok','cancel'] //buttons
// }, function(){
$.ajax({
type: 'post',
url: '/test', // ===== could get id =====
dataType: 'json'
})
.done(function(data){
if(data.code === 1){
debugger // ===== could't get id ? ======
layer.msg('delete success', {icon: 1});
}else{
layer.msg('success fail', {icon: 1});
}
})
.fail(function(err){
layer.msg('success fail', {icon: 1});
})
// })
})
I use $.ajax().done(). when done fired, could't get outer variable "id" in line about "debugger" ?
我使用$ . ajax().done()。当你被解雇时,你无法在“调试器”中得到外部变量“id”吗?
For reslove "It looks like your post is mostly code; please add some more details. ", add some words. Actually, I explain this problem is clear.
对于reslove,看起来你的文章大部分是代码;请补充一些细节。”,添加一些文字。实际上,我解释这个问题很清楚。
1 个解决方案
#1
2
The Javascript interpreter will only capture variables in a closure if they're actually used in the inner function.
如果在内部函数中实际使用了变量,Javascript解释器将只捕获闭包中的变量。
If you change your function to use id
anywhere, it will appear in the debugger.
如果您将函数更改为在任何地方使用id,它将出现在调试器中。
更多相关文章
- 通过调用返回参数的本地函数来构建Ajax Data部分
- Jquery ajax回调函数不执行
- JQUERY组装对象并调用自身函数改变自己的属性
- Angular ng-show不会根据函数返回值显示/隐藏
- 未捕获的ReferenceError:函数未定义,它标记
- 我怎么能用javascript编写这个简短的函数?
- 你能告诉为什么javascript函数没有在profile.php的提交按钮中执
- 使用window.open将变量传递给新的HTML
- JQuery函数不能用于初始触发器