简体   繁体   English

jQuery Selector,用于以编程方式生成的内容的ID

[英]jQuery Selector for ID of programatically generated content

So, I am experimenting with jQuery and I made this (btw sati means hours): UPDATED 因此,我正在尝试使用jQuery,并做到了这一点(顺便说一句,意思是小时): 更新

<script>
$(document).ready(function(){
    window.sati = 1;
    $("#addSat").click(function(){
        var sat = window.sati;
        $('form#form1').append('<span class="hour' + sat + '"><span class="numHour">' + sat + '</span><input type="text" value="" name="subject"/><button type="button" class="down">&darr;</button><button type="button" class="up">&uarr;</button></span><br/>');
        window.sati+=1;
    });
    $("form#form1 > span[class^='hour'] > button").on("click",(function(){
        alert("!");
    }));
});
</script>

As you see I am dynamically adding fields to the form, and that works well. 如您所见,我正在向表单中动态添加字段,并且效果很好。 It is the second function that gives me problems. 这是第二个给我带来麻烦的功能。 That should be selecting the up and down buttons in the generated form span input thingy. 那应该是在生成的表单中选择向上和向下按钮,跨度输入内容。 Could you tell me what am I doing wrong? 你能告诉我我在做什么错吗?

IDs have to be unique, that's why your code fails on the second row. ID必须是唯一的,这就是为什么您的代码在第二行失败的原因。

Use classes instead. 改用类。

Also, your click() function might be looking for an element that's not created yet. 另外,您的click()函数可能正在寻找尚未创建的元素。 Use on() instead, it works with dynamically added content. 使用on()代替,它可以处理动态添加的内容。


UPDATE 更新

So I am using classes now but it still doesn't work! 因此,我现在正在使用类,但仍然无法正常工作! $("form#form1 > span[class^='hour'] > button").on("click",(function(){ alert("!"); }));

You needed to fix your on() selector: 您需要修复on()选择器:

 $(document).ready(function(){ window.sati = 1; $("#addSat").click(function(){ var sat = window.sati; $('form#form1').append('<span class="hour' + sat + '"><span class="numHour">' + sat + '</span><input type="text" value="" name="subject"/><button type="button" class="down">&darr;</button><button type="button" class="up">&uarr;</button></span><br/>'); window.sati+=1; }); $("form#form1").on("click", "span[class^='hour'] > button",function(){ alert("!"); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="form1"></form> <button id="addSat">Dodaj Sat</button> 

I created a working fiddle for you. 我为您创建了一个工作提琴。

http://jsfiddle.net/8dtauh8e/1/ http://jsfiddle.net/8dtauh8e/1/

$(document).ready(function(){
    window.sati = 1;
    $("#addSat").click(function(){
        var sat = window.sati;
        var item = $('<span class="hour' + sat + '"><span class="numHour">' + sat + '</span><input type="text" value="" name="subject"/><button type="button" class="down">&darr;</button><button type="button" class="up">&uarr;</button></span><br/>');

        $('form#form1').append(item);

        item.find('button').on('click', function(){
         alert('here - ' + $(this).prop('class') + ' ' + sat);
        });

        window.sati+=1;
    });
});

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

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