JQuery$.post第二次点击不起作用

JQuery $.post Not working on the second click

本文关键字:不起作用 第二次 post JQuery      更新时间:2024-06-20

感谢您查看:

我所做的是读取多个值,如name和class,以确定被点击的内容,然后将其作为参数发送到服务器上的PHP,这样我就可以切换其class和name值。

这样做效果很好,第一次点击时会切换类,但第二次点击元素时,它就不起作用了。查看chrome的控制台,我可以看到POST结果是成功的,但$.POST回调函数不再运行。

$('a').click(function() {
            if($(this).attr("name")=="Config"){
                return;
            }
            else if ($(this).attr("name")=="Return"){
                return;
            }
            else if ($(this).attr("name")=="Temperatura"){
                return;
            }
            else{

            var parameters = {};                            
                parameters.devicename   =   $(this).attr("name").split('_')[0];
                parameters.devicevalue  =   $(this).attr("name").split('_')[1];
                parameters.devicestate  =   parseInt($(this).html());
                alert (parameters.devicestate);
                var selector;
                if ($(this).hasClass("Luz_On") || $(this).hasClass("Luz_Off")){
                selector = "Luz";   
                $(this).attr('id','ajaxelement');
                alert("entro");
                alert($(this).attr('name'));
                alert(selector);
                alert($(this).attr('id'))
                }
                else if ($(this).hasClass("Cortina_Up") || $(this).hasClass("Cortina_Down")){
                selector = "Cortina";           
                $(this).attr('id','ajaxelement');                   
                }
                else if ($(this).hasClass("Temperatura")){
                selector = "Temperatura";
                $(this).attr('id','ajaxelement');
                }
                else if ($(this).hasClass("Rewind")){
                selector = "Rewind";
                $(this).attr('id','ajaxelement');
                }
                else if ($(this).hasClass("Play")){
                selector = "Play";
                $(this).attr('id','ajaxelement');
                }
                else if ($(this).hasClass("Pause")){
                selector = "Pause";
                $(this).attr('id','ajaxelement');
                }
                else if ($(this).hasClass("Forward")){
                selector = "Forward";
                $(this).attr('id','ajaxelement');
                }
            $.post(
            "http://localhost/AplicacionWeb/test.php", 
            parameters,
            function(data){
            if (data.valid=='true')
            {
                switch(selector)
                {
                    case "Luz":
                        alert("case");
                        if ($('#ajaxelement').attr('name') == "Luz_On"){
                        alert("caseif1");
                        $('#ajaxelement').removeClass("Luz_On").addClass("Luz_Off");
                        $('#ajaxelement').attr('name','Luz_Off');
                        $('#ajaxelement').removeAttr('id');
                        }
                        if ($('#ajaxelement').attr('name') == "Luz_Off"){
                        alert("caseif2");
                        $('#ajaxelement').removeClass("Luz_Off").addClass("Luz_On");
                        $('#ajaxelement').attr('name','Luz_On');
                        $('#ajaxelement').removeAttr('id');
                        }


                    break;
                    case "Cortina":
                    break;
                    case "Temperatura":
                    break;
                    case "Rewind":
                    break;
                    case "Play":
                    break;
                    case "Pause":
                    break;
                    case "Forward":
                    break;
                    default:
                        alert("C'est la vie");
                }


            }//data.valid
            }, 
            "json"
            );
            }

        });  // end click

您将post请求配置为期望服务器http://localhost/AplicacionWeb/test.php返回有效的JSON。如果已验证请求成功返回,请检查数据是否符合指定的dataType。如果响应数据无效,jQueryajax请求将不会运行success/done回调。