简体   繁体   English

选择框上的jQuery AJAX

[英]jQuery AJAX on select box

I'm trying to use ajax on a select box and change html elements with matching id's on event of change in select box. 我试图在选择框上使用ajax并在选择框发生更改时更改具有匹配ID的html元素。 I have a code like below but it does not seem to work and rather generates 'Uncaught ReferenceError: data is not defined' 我有类似下面的代码,但它似乎无法正常工作,而是生成“ Uncaught ReferenceError:未定义数据”

Below is the code that I am using. 下面是我正在使用的代码。 Is there something wrong with this? 这有什么问题吗?

$('#PolicyCategory').live('change',function(){
    var policy = $('#PolicyCategory').val();
    var form_data = {
        category_id: policy,
        ajax: '1'       
    };

    console.log();
    alert(data.conn);

    $.ajax({
        url: "http://coverbuddy.favstay.com/users/category_ajax",
        type: "POST",
        dataType: "json",
        cache: false,
        data: form_data,
        success: function(data){
            alert(data);
        }
    });
});

这意味着变量data未在脚本中定义,而是在这里引起的:

 alert(data.conn);

The error comes up from 错误来自

alert(data.conn);

Where you don't have defined variable data as far as we can see from your code. 据我们从您的代码可以看到的地方,您还没有定义变量data Remove this line or define the variable before you use it. 在使用它之前,请删除此行或定义变量。

You try to to use data where it is not defined: alert(data.conn); 您尝试在未定义的地方使用数据: alert(data.conn); , which naturally gives you the error ,这自然会给您错误

Yes you need to write the " alert(data.conn);" 是的,您需要编写“ alert(data.conn);” with in the success function.. 具有成功功能。

$('#PolicyCategory').live('change',function(){
    var policy = $('#PolicyCategory').val();
    var form_data = {
        category_id: policy,
        ajax: '1'       
    };
    console.log();


    $.ajax({
        url: "http://coverbuddy.favstay.com/users/category_ajax",
        type: "POST",
        dataType: "json",
        cache: false,
        data : form_data,
        success: function(data){
            alert(data);
            alert(data.conn);
        }
    });
});

that's because of the alert(data.conn); 那是因为有alert(data.conn);

remove it and it will work 删除它,它将起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM