选择动态ID JQuery [英] Selecting Dynamic ID JQuery
问题描述
我需要使用JQuery选择动态ID,一旦我选择它,然后我需要对它做一些操作。这是我拥有的HTML:
< input id =content_photos_attributes_1355755712119_imagename =content [photos_attributes] [1355755712119] [image]size =30type =file>
请注意id值,text始终是相同的,但数字会改变(我没有控制权)
我需要做的是为该元素点击创建。这是我到目前为止,并没有工作。
< script type =text / javascript>
jQuery.noConflict();
jQuery(input [id * ='content_photos_attributes _'])。click(function(){
alert(点击图片删除);
});
< / script>
无论我是通过ID还是通过名称来选择该元素,都没有什么区别。
正如我所看到的,它应该在 $(document).ready();
处理程序及其动态ID,因此,您必须使用 .on()
处理程序来选择选择器:
$(document).ready(function(){
jQuery(document).on('click','input [id ^ ='content_photos_attributes_]', function(){
alert(单击图像删除);
});
});
试试看看是否有帮助。
I need to select dynamic id using JQuery, and once I select it then I need to do some action on it. This is the HTML that I have:
<input id="content_photos_attributes_1355755712119_image" name="content[photos_attributes][1355755712119][image]" size="30" type="file">
Please note the id value, text is always the same however the number changes (I do not have control over that change).
What I need to do is to create on click for that element. This is what I got so far, and it is not working.
<script type="text/javascript">
jQuery.noConflict();
jQuery("input[id *= 'content_photos_attributes_']").click(function() {
alert("Image deletion is clicked");
});
</script>
It really makes no difference whether I select that element by ID or by its name.
As i see this it should be in the $(document).ready();
handler and its a dynamic id so for this you have to use .on()
handler to select the selector:
$(document).ready(function(){
jQuery(document).on('click', 'input[id^="content_photos_attributes_"]', function() {
alert("Image deletion is clicked");
});
});
try this and see if helps.
这篇关于选择动态ID JQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!