Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery选择动态ID_Jquery_Html - Fatal编程技术网

jQuery选择动态ID

jQuery选择动态ID,jquery,html,Jquery,Html,我需要使用JQuery选择动态id,一旦我选择了它,我就需要对它执行一些操作 <input id="content_photos_attributes_1355755712119_image" name="content[photos_attributes][1355755712119][image]" size="30" type="file"> HTML包含ID和Name属性。我不在乎哪一个我可以选择,只要它被选中。 这就是我迄今为止所尝试的,但没有成功 <scr

我需要使用JQuery选择动态id,一旦我选择了它,我就需要对它执行一些操作

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

HTML包含ID和Name属性。我不在乎哪一个我可以选择,只要它被选中。 这就是我迄今为止所尝试的,但没有成功

<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);

});

jQuery.noConflict();
jQuery(“添加照片产品”)。单击(函数(){
警报(“处理程序fooor.click()调用”);
//变量字段=$(“输入[id*='content\u photos\u attributes\u']);
变量字段=$('input[name*='content']);
警报(现场);
});
第一个选择器工作,我可以轻松地抓取它。一旦它被执行,DOM就会填充一个我可以得到的(在HTML代码下)

我尝试过各种组合,但它总是空的


如何执行此操作?

您运行了
jQuery.noConflict()
,这意味着$不是jQuery。尝试使用
jQuery
而不是运行
jQuery.noConflict()
,这意味着$不是jQuery。尝试使用
jQuery
而不是
$

尝试替换此

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

   jQuery("#content_photos_attributes_1355755712119_image").click(function() {
您没有生成html代码中显示的
id

要更好地运行您的测试,请执行easy alert以测试它是否有效

那样

    alert(1);     // this will alert 1
   var myid = "content_photos_attributes_"+your_variable+"_image" ;
             // note that your_variable is this which generates 1355755712119 

    jQuery("#"+myid).click(function() {
编辑2>

如果你的id像一个变量,那么你也可以这样做

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

    jQuery("#"+myid).click(function() {
试着换掉这个

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

   jQuery("#content_photos_attributes_1355755712119_image").click(function() {
您没有生成html代码中显示的
id

要更好地运行您的测试,请执行easy alert以测试它是否有效

那样

    alert(1);     // this will alert 1
   var myid = "content_photos_attributes_"+your_variable+"_image" ;
             // note that your_variable is this which generates 1355755712119 

    jQuery("#"+myid).click(function() {
编辑2>

如果你的id像一个变量,那么你也可以这样做

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

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

谢谢你的评论@Goodmood如果我使用你的第二个例子,它将不起作用,因为这个数字总是动态的,但是文本总是相同的。所以我需要solutkon来实现这个:jQuery(“#内容#照片(u属性)xxxxxxxxxx(u图像))。单击(函数(){X是akways更改的内容。我如何控制该项目并在其上运行onclick?谢谢!你能告诉我这是什么XXX?这是什么变量吗?谢谢你的评论。@Goodmood如果我使用你的第二个示例,它将不起作用,因为该数字始终是动态的,但是文本始终是相同的。因此我需要solutkon来表示this:jQuery(“#内容#照片(属性)xxxxxxxxxx(图像)”)。单击(函数(){X是已更改的内容。如何控制该项目并在其上运行一次单击?谢谢!你能告诉我这是什么XXX吗?这是什么变量?可能的重复可能的重复