簡體   English   中英

jQuery $ .post不適用於第二次單擊

[英]JQuery $.post Not working on the second click

感謝您檢查以下內容:

我正在做的是讀取多個值(例如名稱和類)來確定要單擊的值,然后將其作為參數發送到服務器上的PHP,以便我可以切換其類和名稱值。

這很好用,並在我第一次單擊它時切換了類,但是第二次我單擊該元素時,它不再起作用。 查看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

您配置了發布請求,以期望服務器http://localhost/AplicacionWeb/test.php返回有效的JSON 如果您驗證請求已成功返回,則檢查數據是否符合指定的dataType 如果響應數據無效,則jQuery ajax請求將不會運行成功/完成回調。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM