jquery变量值表示未定义

时间:2013-06-28 19:09:59

标签: jquery

变量$ x10Device在if语句中说未定义为什么?在函数的其余部分,它有一个值,如何让它保持它的值。

$("button.checkStatus").click(function () {
    //This Ajax checks the current on/off status of the passed X10 code
    $('.checkStatus').each(function (i, obj) {
        $x10Device = $(this).data("x10");
        //var data = "url=http://192.168.0.34:81/tenHsServer/tenHsServer.aspx?t=ab&f=DeviceStatus&d=C5" //& $x10Device ; //this is passed in the device toggle

        var data = "url=http://192.168.0.34:81/tenHsServer/tenHsServer.aspx?t=ab&f=DeviceStatus&d=" + $x10Device; //this is passed in the device toggle

        $.ajax({
            url: "urlencode.php",
            data: data,
            type: "POST",
            success: function (data) {
                myd = $('<span />').html(data).find("#Result").text();
                var Nmyd = myd.charAt(3);
                if (Nmyd == '2') {
                    $($x10Devic).data('src', 'lightbulbon.png')
                } else {
                    $('img').attr('src', 'lightbulboff.png')
                };
            },
            error: function (request, status, error) {
                alert(request.responseText);
            }
        });
    });
});

2 个答案:

答案 0 :(得分:1)

不确定这是否是整个问题,但你有一个拼写错误:

$x10Device$x10Devic

答案 1 :(得分:0)

$x10Device的定义是错误的(正如上下文所暗示的那样)。您将使用两个变量消除这些错误以避免混淆:$Device = $(this); $x10 = $Device.data('x10');

$("button.checkStatus").click(function(){
    $('.checkStatus').each(function (i,obj){
        /**
         * These changes assume that $x10Device was NOT a global variable
         */
        var $Device = $(this),
        $x10 = $Device.data("x10"),
        data = "url=http://192.168.0.34:81/tenHsServer/tenHsServer.aspx?t=ab&f=DeviceStatus&d=" + $x10;

        $.ajax({
            url: "urlencode.php",
            data: data,
            type: "POST",
            success: function (data) {
                myd = $('<span />').html(data).find("#Result").text();
                var Nmyd = myd.charAt(3);
                if (Nmyd == '2') {
                    $Device.data('src', 'lightbulbon.png')
                } else {
                    $('img').attr('src', 'lightbulboff.png')
                };
            },
            error: function (request, status, error) {
                alert(request.responseText);
            }
        });
    });
});