简体   繁体   English

Javascript在不存在的代码上添加onclick事件

[英]Javascript add onclick event on nonexistent code

i'm facing an issue in php/javascript. 我在php / javascript中遇到问题。 I have a table with a tons of informations and you can press many buttons for displaying many informations. 我的桌子上有很多信息,您可以按许多按钮来显示很多信息。

When you press this button, your web browser will display a modal and execute javascript code for querying the database. 当您按下此按钮时,您的Web浏览器将显示模式并执行用于查询数据库的javascript代码。 In this modal window, you have a button "delete" referencing itself... The goal is when I press the delete button, I keep the modal but the content is refresh... 在此模式窗口中,您有一个引用自身的按钮“删除” ...目标是当我按下删除按钮时,我保持模式,但内容刷新了...

You will find below some parts of my code: 您会在我的代码的以下部分找到:

The button inside the table: <button data-toggle="modal" href="#scommand" data-id='. $user['id'].' class="suser">display</button>' 表格内的按钮: <button data-toggle="modal" href="#scommand" data-id='. $user['id'].' class="suser">display</button>' <button data-toggle="modal" href="#scommand" data-id='. $user['id'].' class="suser">display</button>' <button data-toggle="modal" href="#scommand" data-id='. $user['id'].' class="suser">display</button>' ; <button data-toggle="modal" href="#scommand" data-id='. $user['id'].' class="suser">display</button>'

The javascript: (row contains the sql answer) javascript :(行包含sql答案)

$(document).on("click", ".suser", function (e) {
var obj = $.parseJSON(row);
$.each(obj, function(){
 <a  class="suser" onclick="delSum(sum_id)" data-id="'+ user_id+ '"'>delete</a>
}

The problem faced is whatever if i click or not all data are deleted... 面临的问题是无论我单击还是不删除所有数据...

So I tried to do this: 所以我尝试这样做:

            $(document).on("click", ".suser", function (e) {
    var obj = $.parseJSON(row);
    $.each(obj, function(){
    <a id="toDel+'this['sum_id'] +'" class="suser" onclick="delSum(sum_id)" data-id="'+ user_id+ '"'>delete</a>
a = document.getElementById('#toDel'+this['sum_id']);
a.on('click',delSum(this['sum_id']));

But it doesn't work and I get this error "a is null" I guess it's because the code doesn't exist yet... 但这不起作用,并且我收到此错误“ a为null”,我猜是因为该代码尚不存在...

So I don't know how to do? 所以我不知道该怎么办? Any idea? 任何想法?

Best regards, 最好的祝福,

You can write : 你可以写 :

if($("#toDel'+this['sum_id']") != null)
{
    `enter onclick code here`;
}

Finally I fixed the issue, I don't know why it didn't work and I don't know why it works now. 最终,我解决了这个问题,我不知道为什么它不起作用,现在也不知道为什么。 The difference is: 区别在于:

$(document).on("click", ".suser", function (e) {
var obj = $.parseJSON(row);
$.each(obj, function(){
 <a  class="suser" onclick="file(url + sum_id)" data-id="'+ user_id+ '"'>delete</a>
}

I call directly the function querying the php script instead of calling transitional function... 我直接调用查询php脚本的函数,而不是调用过渡函数...

memes 模因

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

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