简体   繁体   English

不能与JQuery中的attr()一起使用“ onclick”

[英]It does not work “onclick” with attr () from JQuery

I'm trying to add an "onclick" event with their respective function from js JQuery to an interactive table td I'm doing in another .php file, the problem is not executed in the place where I want to do it but if the same instruction is executed correctly within an AJAX request a few lines below, will show you the code: 我正在尝试将js JQuery各自功能的“ onclick”事件添加到我正在另一个.php文件中执行的交互式表td中,该问题未在我想要执行的地方执行,但是如果下面几行中的AJAX请求中正确执行了同一条指令,将向您显示代码:

This instruction is giving me the problem: 这条指令给我的问题是:

$(td).attr("onclick","agregar_pensum_etapa2 (this,'"+subject+"','"+level+"','"+stage+"');");

And this, the function I want to run with the "onclick" 这就是我要通过“ onclick”运行的功能

function agregar_pensum_etapa2(td,subject,level,stage){

        $(document).ready(function(){
            // Capture variables.
            var id_periodo = $("#id_periodo").val();
            var id_asignatura = subject;
            var id_nivel = level;
            var id_etapa = stage;
            var id_mencion = null;
            if (level> 3) {
               id_mencion = 0;
            }

            // Modifiable column.
            var tr = $(td).parent();
            var index_td = $(td).index();

            // First field
            var valor_anterior = $(td).text();
            $(td).html("<img src =" images / save_64.png 'width = '16' height = '16 '> & nbsp; & nbsp; & nbsp;' + '<input value = "' + valor_anterior + '" type = "text" style = "width: 40px; border: 1px solid #aaa;" onkeypress = "return soloNumeros (event);" maxlength = "2"> ');
            $(td).removeAttr("onclick");
            $(td).find("input").focus();

            // Second field

            var valor_anterior_cs = $(tr).find("td:eq("+(index_td+1)+")").text();
            var checked_cs = "";
            if (valor_anterior_cs === "X"){checked_cs = "checked"}
            $ (tr) .find ("td: eq (" + (index_td + 1) + ")") html ("<input type = 'checkbox'" + checked_cs + ">").

            // Third field

            var valor_anterior_hum = $(tr).find("td:eq("+(index_td+2)+")").text();
            var checked_hum  = "";
            if(valor_anterior_hum === "X") {checked_hum = "checked"}
            $(tr).find("td:eq("+(index_td+2)+")").html("<input type = 'checkbox'"+checked_hum+">");

           / ************************************************* *********** /


            $(td).find("img").click(function(){

                var hora_asignatura  = $(td).find("input").val();

                var mencion_cs = "NO";
                if($(tr).find("td:eq("+(index_td+1)+")").find("input").is ("checked")){mencion_cs = "YES";}

                var mencion_hum = "NO";
                if($(tr).find("td:eq("+(index_td+2)+")").find("input").is("checked")){mencion_hum = "YES";}


                if(hora_asignatura === ""){

                   if(valor_anterior != ''){

                     $(td).html(valor_anterior);      
                     $(tr).find("td:eq("+index_td+1)+")").text(valor_anterior_cs);
                     $(tr).find("td:eq("+(index_td+2)+")").text(valor_anterior_hum);
                   }else{

                     $(td).html("");
                     $(tr).find("td:eq("+(index_td+1)+")").text("");
                     $(tr).find("td:eq("+(index_td+2)+")").text("");
                   }

                   \\// --> HERE IS NOT WORKING <-- \\//
                   $(td).attr("onclick","agregar_pensum_etapa2(this,'"+subject +"','"+level+"','"+stage+"');");
                }else if(hora_asignatura == "0"){

                   if(valor_anterior! = ''){

                      $(td).html(valor_anterior);
                      $(tr).find("td:eq("+(index_td+1)+")").text (valor_anterior_cs);
                      $(tr).find("td:eq("+(index_td+2)+")").text (valor_anterior_hum);
                   }else{

                      $(td).html("<img src =" images / diagonal.png 'height = '16' style = 'width: 15px ">");
                      $(tr).find("td:eq("+(index_td+1)+")").text("");
                      $(tr).find("td:eq("+(index_td+2)+")").text("");
                   }

                   \\// --> HERE IS NOT WORKING <-- \\//
                   $(td).attr("onclick","agregar_pensum_etapa2(this,'"+subject+"','"+level+ "','"+stage+"');");
                }else{

                    $.ajax({
                        async: true,
                        cache: false,
                        dataType: "html"
                        type: 'POST'
                        url: "../Controlador/CtrlPensum.php"
                        data: {

                            id_periodo: id_periodo,
                            id_asignatura: id_asignatura,
                            id_nivel: id_nivel,
                            id_etapa: id_etapa,
                            hora_asignatura: hora_asignatura,
                            mencion_cs: mencion_cs,
                            mencion_hum: mencion_hum,
                            id_mencion: id_mencion,
                            record: "register"
                        },
                        success: function (response) {
                            //alert (response);
                            if($.trim(answer) === "1") {

                               $(td).html(hora_asignatura);

                               var marcar_cs_x = "";
                               if(mencion_cs === "SI"){marcar_cs_x = "X";}

                               var marcar_hum_x = "";
                               if(mencion_hum === "SI"){marcar_hum_x = "X";}

                               $(tr).find("td:eq("+(index_td+1)+")").html (marcar_cs_x).
                               $(tr).find("td:eq("+(index_td+2)+")").html (marcar_hum_x).

                               \\// --> HERE IT WORKS <-- \\//
                               $(td).attr("onclick", "agregar_pensum_etapa2 (this,'"+subject+"','"+level+"','"+stage+"');");
                               cargarTablaResumen ();

                            } Else {
                                alert ("Error SQL statement is not executed." + response);
                            }
                            //tr.fadeOut(500).fadeIn(500);
                        },
                        beforeSend: function () {}
                        Error: function (objXMLHttpRequest) {}
                    });
                }
            });
        });
}

I try this: 我尝试这样:

$(td).click(function(){
     $(td).attr("onclick","agregar_pensum_etapa2 (this,'"+subject+"','"+level+ "','"+stage+"');");
});

And in the html it prints correctly but does not work, like he was not the "onclick" there. 并且在html中可以正确打印,但无法正常工作,就像他不是那里的“ onclick”一样。 And the function if it works, I put the onclick to that function when I load the page and if it works, but when I click the td and give back to click to restore does not restore the onclick. 如果该函数有效,则在加载页面时,如果将onclick放到该函数上,并且可以正常工作,但是当我单击td并退还给单击以进行还原时,不会恢复onclick。

Instead of setting the onclick attribute, why not do use .click() again with closures? 为何不设置onclick属性,为什么不对闭包再次使用.click() So your code would be something like... 因此您的代码将类似于...

$(td).click(function(){
    var td = this;
    $(td).click(function () {
         agregar_pensum_etapa2(td,subject,level,stage);
    });
});

是否用小写字母“ d”写$(Document)而不是$(document)。

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

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