动态添加元素javascript / jquery上的每个循环的click事件

时间:2018-11-29 10:02:17

标签: javascript jquery events each

我的问题是$('input[name=isDefault]').length等于2,但是每个都只运行一个。

$(saveProduct).click(function (e) {
    $('input[name=isDefault]').each(function (index) {
       var that = this;
       console.log(index, 'index');
       console.log($('input[name=isDefault]').length);
       // do something...
    })
})

$('input[name=isDefault]')在另一个逻辑期间动态添加obj。 我知道有一种类似$(upper container).on("event", dynamic obj inside upper container)的解决方案-但这导致click事件几乎无处不在,因为.on会将事件附加在big area container

编辑: 我出于测试目的进行了一些更改:

var imgs = imgs = $('div[name=fileContainer]');
console.log('before loop', imgs.length); // = 1
for (var i = 0; i < imgs.length; i++) {
    console.log('inside loop', imgs.length); // = 1
    //do something - imgs is dynamicly added html element by jquery, when I load page there is 1 element with that name
}

,但是当我在控制台中写入$('div[name=fileContainer]').length;时,它等于2(当然,当我动态添加+ 1个fileContainer元素时)

编辑: 这不是答案,但是我省略了使用上述循环的需要。 在代码的其他地方,我已经有了$(something).on("change", dynamiclyAddedElements function() {})和主要目的……但是!我在其中添加了addintional函数,并使用所需的数据创建了javascipt obj。然后,我可以在上述click事件中使用它们。

1 个答案:

答案 0 :(得分:0)

  

$(上部容器).on(“事件”,上部容器内部的动态obj)-但这导致click事件几乎无处不在,因为.on在大型容器上附加了事件

那是不对的,您可以使用第二个参数来指定选择器

例如

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;




public class Demo2 {



    public static void main(String[] args) throws IOException {
        File sourceCsvFile = null;
        File finalCsvFile = null;
        sourceCsvFile = new File("C:\\MyData\\Input.csv");
        finalCsvFile = new File("C:\\MyData\\Output.csv");
        String line = "";
        String cvsSplitBy = ",";



        BufferedWriter writer = new BufferedWriter(new FileWriter(finalCsvFile));


        try (BufferedReader br = new BufferedReader(new FileReader(sourceCsvFile))) // read the actual Source downloaded csv file which contains double quotes 
        {
            line = br.readLine();  // read only first line
            String newFileLine = "\"UniqueID\"" + cvsSplitBy + line + cvsSplitBy + "\"IsDelete\"" + cvsSplitBy + "\"Rootpid\"";
            writer.write(newFileLine);   // will be written as first line in new csv
            writer.newLine();  //  go to next line for writing next lines


            while ((line = br.readLine()) != null)  // this loop to write data for all lines except headers
            {

                newFileLine = "\"\"" + cvsSplitBy + line  + cvsSplitBy + "\"\"" + cvsSplitBy + "\"\"";
            //[enter image description here][1] newFileLine = newFileLine.replace("\"", "");  // when replace is used double quotes is gone, but then comma(,) within data is considered as new column and so data shifts to next column 
                writer.write(newFileLine);
                writer.newLine();

            }

        }

        writer.close();





    }
}

会将点击处理程序添加到(也动态添加)$('#upper_container').on('click', '.saveProduct', function(){...}) ,而不是您声明的.saveProduct