代码之家  ›  专栏  ›  技术社区  ›  Pez Cuckow

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

  •  22
  • Pez Cuckow  · 技术社区  · 15 年前

    我正在使用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");
        }
    });
    

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

    1 回复  |  直到 14 年前
        1
  •  37
  •   Nick Craver    15 年前

    是因为 this 不是你以前处理的,现在是 ajax context option of $.ajax() 这样地:

    $.ajax({
      context: this,
      url: "ajax/save_text.php",
      ...
    

    在你的回拨中指的是相同的 当你打电话的时候 $.ajax() . 或者,只需保留对

    另外,你需要调整 $(this).value ,你可能是说 this.value $(this).val() .