jQuery"无法读取未定义的属性'defaultView'错误

Pez*_*kow 24 ajax jquery

我正在使用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");
    }
});
Run Code Online (Sandbox Code Playgroud)

但是每次成功(html == 1)时,控制台都会抛出错误"Uncaught TypeError:无法读取未定义的属性'defaultView'并且警报永远不会发生......?

谷歌似乎没有太多关于这个错误和jQuery的信息,谁知道原因?

Nic*_*ver 37

这是因为this不是你以前处理过的,它现在是ajaxjQuery对象,添加如下context选项$.ajax():

$.ajax({
  context: this,
  url: "ajax/save_text.php",
  ...
Run Code Online (Sandbox Code Playgroud)

this回调中的这种方式与this你打电话时的方式相同$.ajax().或者,只需this在单独的变量中保留引用.

此外,你需要调整$(this).value,你可能意味着this.value$(this).val().