jQuery选择动态ID [英] jQuery selecting dynamic ID

查看:102
本文介绍了jQuery选择动态ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用JQuery选择动态ID,一旦选择它,就需要对其进行一些操作.

I need to select dynamic id using JQuery, and once I select it then I need to do some action on it.

<input id="content_photos_attributes_1355755712119_image"   name="content[photos_attributes][1355755712119][image]" size="30" type="file">

HTML包含ID和Name属性.我不在乎可以选择哪一个. 到目前为止,这是我尝试过的,但没有成功.

The HTML contains both ID and Name attribute. I dont care which one I can select as long it is selected. This is what I have tried so far, without success.

<script type="text/javascript">
jQuery.noConflict();
jQuery("#add_photo_product").click(function() {

alert("Handler foooor .click() called.");

//var field = $("input[id *= 'content_photos_attributes_']");
var field = $('input[name*="content"]');
alert (field);

});

第一个选择器起作用了,我可以很容易地抓住它.一旦执行,DOM就会被我可以掌握的(在HTML代码下)填充.

The first selector works, and I can grab it easily. As soon as it is executed, then DOM is populated with the one that I can get hold of (Under HTML code).

我尝试了各种组合,但始终为空.

I have tried various combinations, but it is always null.

我该怎么做?

推荐答案

尝试替换此

   jQuery("#add_photo_product").click(function() {

以此

   jQuery("#content_photos_attributes_1355755712119_image").click(function() {

您未制作HTML代码中显示的id

you are not making the id which is shown in your html code

为了更好地进行测试,您可以做一个简单的警报,只是测试它是否有效

to run you your test better do easy alert just to test if it works

喜欢

    alert(1);     // this will alert 1

EDIT2>

如果您的ID像变量一样,那么您也可以这样做

if you have the id like a variable then you can also do it like that

   var myid = "content_photos_attributes_"+your_variable+"_image" ;
             // note that your_variable is this which generates 1355755712119 

    jQuery("#"+myid).click(function() {

这篇关于jQuery选择动态ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆