简体   繁体   English

Click事件已添加到附加的html; 元素未按预期工作

[英]Click event added to appended html; element not working as anticipated

OK I am sure I am doing something simple fundamentally wrong but am unable to resolve this issue on my own. 好的,我确定我在做一些根本上根本不对的事情,但是我自己无法解决此问题。 I have tried googling and searching this forum and reading through the relevant parts of the jquery documentation. 我尝试使用Google搜索和搜索此论坛,并通读jquery文档的相关部分。

If you need any further information please let me know. 如果您需要任何其他信息,请告诉我。 Many thanks in advance. 提前谢谢了。

What I am trying to do: 我正在尝试做的是:

I have a table showing details of various tests at the bottom of which I have a button to ad a new test. 我有一个表格,其中显示了各种测试的详细信息,在其底部,我有一个按钮可以进行新的测试。 As each test comprises several rows of the table I have a template in a hidden div which I have cloned and appended to the live table. 由于每个测试都包含表的几行,因此我在一个隐藏的div中有一个模板,该模板已克隆并附加到活动表中。 I am the using jquery to add appropriate classes and ids based on a variable integer for the sake of uniqueness. 为了唯一起见,我正在使用jquery基于可变整数添加适当的类和id。 I am also attempting to add a click event to an image within one of the table cell to delete the rows relating to that particular test. 我还试图将click事件添加到表单元格之一中的图像上,以删除与该特定测试有关的行。

My current function 我目前的功能

function newTest(sampleID){
    var testID = $("#testCount").val();
    $("#newTest tr").clone(true)
        .addClass("test"+testID)
        .appendTo("#testsTable"+sampleID);
    $(".newdelbox:first")
        .attr("id","testDelete"+testID)
        .addClass("delbox")
        .removeClass("newdelbox");
    $(".test"+testID).on('click',"#testDelete"+testID,delSample(testID));
    testID++;
    $("#testCount").val(testID);
}

What I need to happen 我需要发生的事情

Function to be called when image is clicked. 单击图像时要调用的函数。

What is happening 怎么了

Function is called only when script is assigned (not clicked). 仅当分配了脚本(未单击)时才调用功能。 Function will not subsequently run when clicked. 单击该功能以后将不会运行。 I am seeing no errors within the console. 我在控制台内看不到任何错误。

What I have tried 我尝试过的

chained to preceeding code: 链接到以下代码:

.click(delSample(testID));
.on("click",delSample(testID));
.bind("click",delSample(testID));

As new line within function: 作为函数中的新行:

$(".test"+testID).on('click',"#testDelete"+testID,delSample(testID));
document.getElementById("testDelete"+testID).onclick(delSample(testID));
document.getElementById("testDelete"+testID).addEventListener("click",delSample(testID),false);

i do not know of your html but i supose something like 我不知道你的HTML,但我认为像

.on("click",function(){
   var current_id = jQuery(this).attr('id');
   delSample(current_id);
});

just locate your id 只要找到你的身份证

try this: 尝试这个:

 .click(function(){
  delSample(testID);
  });

OR 要么

.on("click",function(){
   delSample(testID);
  });

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

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