我正在使用jQuery将一个表单域发布到一个PHP文件中,该文件根据它是否工作而返回1/0……
代码摘录:
$.ajax({
url: "ajax/save_text.php", //Relative?!?
//PHP Script
type: "POST",
//Use post
data: 'test=' + $(this).val(),
datatype: 'text',
//Pass value
cache: false,
//Do not cache the page
success: function(html) {
if (html == 1) {
$(this).hide().siblings('span').html($(this).value).show();
alert("awesome!");
} else alert('It didn\'t work!');
},
//Error
error: function() {
alert("Another type of error");
}
});
但是,每次成功时(html == 1),控制台都会抛出错误。
未捕获TypeError:无法读取未定义的属性'defaultView‘“
而警报永远不会发生...?
谷歌似乎没有太多关于这个错误和jQuery的信息,谁知道原因呢?
发布于 2010-10-15 02:16:46
这是因为this
不是您以前处理的对象,它现在是ajax
jQuery对象,像这样添加context
option of $.ajax()
:
$.ajax({
context: this,
url: "ajax/save_text.php",
...
这样,回调中的this
将引用与调用$.ajax()
时相同的this
。或者,只需在单独的变量中保留对this
的引用。
此外,您还需要调整$(this).value
,您可能指的是this.value
或$(this).val()
。
https://stackoverflow.com/questions/3936211
复制相似问题