作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
想象一下:
$('#blah').on('click', function(){
var cat_id = $(this).attr('id');
$.ajax({
// stuff...
data: {cat_id: cat_id}, // <--------------------
// stuff...
}).done(function(){
alert(cat_id); // <-------------------- not defined...
});
});
如您所知,jQuery 已弃用了以前的使用类型 $.ajax
,新模式如上所示,位于 done()
函数中的上述代码中,我如何访问cat_id
?在done()函数中,$(this)
不再被识别,cat_id
也不再被识别......
在 jQuery 引入 done()
之前,我们可以轻松访问发送的数据,因为我们使用的是 success:
并且我们仍然可以通过 ajax 函数访问发送的数据.
最佳答案
在您的示例中,您仍然可以在完成函数中访问cat_id,因为它仍在范围内。如果您需要访问 $(this),您需要将其绑定(bind)到完成回调,如下所示:
$('#blah').on('click', function(){
var cat_id = $(this).attr('id');
$.ajax({
// stuff...
data: {cat_id: cat_id}, // <--------------------
// stuff...
}).done(function(){
alert(cat_id); // alerts 'blah'
alert($(this).attr('id')); // also alerts 'blah'
}.bind(this)); // NOTICE THIS
});
关于jquery - $.ajax() 访问 did() 中发送的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15157693/
今天小米米家官方微博宣布,小米手表尊享版开启限量定金预售,3 月 3 日早 10 点起支付尾款。购买即赠 399 元小米真无线蓝牙耳机 Air 2。 小米手表尊享版相比普通版核心没有变化,采用
今天下午,小米官方公布了小爱触屏音箱Pro 8,可以看到触控屏幕尺寸比较大,音箱上“长”了一个平板。据悉,小米小爱触屏音箱Pro 8具有白色的配色设计,下方有一个长条状的扬声器,上方带有一个尺寸比较
我是一名优秀的程序员,十分优秀!