jQuery:在Ajax请求中出现'Uncaught TypeError:Illegal Invocation'-几个元素 [英] JQuery: 'Uncaught TypeError: Illegal invocation' at ajax request - several elements

查看:288
本文介绍了jQuery:在Ajax请求中出现'Uncaught TypeError:Illegal Invocation'-几个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个选择元素,A和B:当A的选择选项更改时,B的选项必须相应地更新. A中的每个元素都隐含着B中的许多元素,这是一对多的关系(A包含国家,B应该包含给定国家中的城市).

I have two select elements, A and B: when A's selected option changes, B's options must be updated accordingly. Each element in A implies many elements in B, it's a one-to-many relationship (A contains nations, B should contain cities located in the given nation).

函数do_ajax应该运行异步请求:

The function do_ajax should run the asynchronous request:

function do_ajax(elem, mydata, filename)
{
    $.ajax({
        url: filename,
        context: elem,
        data: mydata,
        datatype: "html",
        success: function (data, textStatus, xhr) {
            elem.innerHTML = data;
        }
    });
}

为了更新B的选项,我在A的onChange事件中添加了一个函数调用.这是触发(A的)onChange事件时运行的函数:

In order to update B's options I've added a function call in A's onChange event. Here is the function that runs when onChange event (of A) is triggered:

function my_onchange(e) // "e" is element "A"
{
    var sel_B = ... ; // get select element "B"

    // I skipped some code here
    // ...

    var data = {
        'mode': 'filter_city',
        'id_A': e[e.selectedIndex]
    };
    do_ajax(city_sel, data, 'ajax_handler.php');
}

}

我已经阅读了 JQuery文档,其中data可以是一个数组(键值对).如果输入以下内容,则会收到错误消息:

I've read in JQuery docs that data can be an array (key value pairs). I get the error if I put:

var data = {
        'mode': 'filter_city',
        'id_A': e[e.selectedIndex]
};

相反,如果我的数据是字符串,则不会出现该错误:

Instead, I don't get that error if my data is a string:

var data = 'mode=filter_city&id_A=' + e[e.selectedIndex];

但是我需要在服务器端php代码中使用变量的数组版本".

But I need the "array version" of the variable, in my server-side php code.

Uncaught TypeError: Illegal invocation位于"jquery-1.7.2.min.js"文件中,该文件已全部压缩,因此我无法弄清楚代码的哪一部分引发了错误.

The Uncaught TypeError: Illegal invocation is located in the "jquery-1.7.2.min.js" file, which is all compressed, so I couldn't figure out what part of code raised the error.

我是否可以更改代码中的任何设置,以便它接受数据作为关联数组?

Is there any setting I can change in my code so that it accepts data as an associative array?

推荐答案

感谢与 Sarfraz 的交流,我们可以找到解决方案.

Thanks to the talk with Sarfraz we could figure out the solution.

问题是我传递了HTML元素而不是它的值,这实际上是我想要做的(实际上,在我的php代码中,我需要将该值作为查询我的cities表和过滤器的外键正确的条目).

The problem was that I was passing an HTML element instead of its value, which is actually what I wanted to do (in fact in my php code I need that value as a foreign key for querying my cities table and filter correct entries).

所以,而不是:

var data = {
        'mode': 'filter_city',
        'id_A': e[e.selectedIndex]
};

应为:

var data = {
        'mode': 'filter_city',
        'id_A': e[e.selectedIndex].value
};

注意:请检查Jason Kulatunga的答案,它引用了jQuery文档来解释为什么传递一个HTML元素引起了麻烦.

Note: check Jason Kulatunga's answer, it quotes JQuery doc to explain why passing an HTML element was causing troubles.

这篇关于jQuery:在Ajax请求中出现'Uncaught TypeError:Illegal Invocation'-几个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆