-1

变量 $x10Device 在 if 语句中说 undefined 为什么?在其余的功能中它有一个价值,我如何让它保持它的价值。

$("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);
            }
        });
    });
});
4

2 回答 2

1

不确定这是否是整个问题,但您有一个错字:

$x10Device对比$x10Devic

于 2013-06-28T19:12:03.530 回答
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);
            }
        });
    });
});
于 2013-06-28T19:25:00.973 回答