简体   繁体   English

使用jQuery在数组类型ID中未定义值

[英]values are undefined in array type ids using jquery

I am changing src of an image after a colorbox is being closed. 关闭颜色框后,我正在更改图像的src。 When ID of the img tag is like tabImage , it works fine but when I change the id to tabImage[0] it stop working. img标签的ID类似于tabImage ,它可以正常工作,但是当我将ID更改为tabImage[0]它将停止工作。

Working code is as follow : 工作代码如下:

<div class="whBg"><a href="javascript:openIframe('offerImage');">Select Image</a></div>
<form:hidden path="offerImageName" />
<form:hidden path="offerImageRel" />
<form:hidden path="offerImageTitle" />
<form:errors path="offerImage" cssClass="errorInst" />              
<img id="offerImages" src="" style="width: 642px; margin:0px 0 0 220px;" />
<input type="hidden" id="returnImageName" />
<input type="hidden" id="returnImageSrc" />
<input type="hidden" id="returnImageRel" />
<input type="hidden" id="returnImageTitle" />

<script>
  function openIframe(tabName)
  {
    $.colorbox({
      iframe:true, 
      href:"imageListFrame",
      innerWidth:1000, 
      innerHeight:500,
      onClosed: function() { 
        $("#"+tabName+"s").attr("src", $("#returnImageSrc").val());
        $("#"+tabName+"Name").val($("#returnImageName").val());
        $("#"+tabName+"Rel").val($("#returnImageRel").val());
        $("#"+tabName+"Title").val($("#returnImageTitle").val());
      }   
    });    
  }
</script>

When I change IDs of input and img it stop working. 当我更改inputimg ID时,它将停止工作。

<div class="whBg"><a href="javascript:openIframe('toursImage',0);">Select Image</a></div>
<input type="hidden" id="toursImageName[0]" />
<input type="hidden" id="toursImageRel[0]" />
<input type="hidden" id="toursImageTitle[0]" />
<img id="toursImages[0]" name="toursImages[0]" src="" style="width: 642px; margin:0px 0 0 220px;" />
<input type="hidden" id="returnImageName" />
<input type="hidden" id="returnImageSrc" />
<input type="hidden" id="returnImageRel" />
<input type="hidden" id="returnImageTitle" />

<script>
  function openIframe(tabName,id)
  {
    $.colorbox({
      iframe:true, 
      href:"imageListFrame",
      innerWidth:1000, 
      innerHeight:500,
      onClosed: function() { 
        $("#"+tabName+"s["+id+"]").attr("src", $("#returnImageSrc").val());
        alert($("#"+tabName+"s["+id+"]").attr("src"));
        $("#"+tabName+"Name["+id+"]").val($("#returnImageName").val());
        $("#"+tabName+"Rel["+id+"]").val($("#returnImageRel").val());
        $("#"+tabName+"Title["+id+"]").val($("#returnImageTitle").val());
      }   
    });    
  }
</script> 

I have double checked that returnImageName , returnImageSrc etc are getting right values. 我已经仔细检查过returnImageNamereturnImageSrc等正在获取正确的值。 I tried to access value of #toursImages[0] using jquery but I am getting undefined in alert. 我尝试使用jquery访问#toursImages[0]值,但在警报中undefined

You have to escape the [] - Brackets with \\\\ 您必须使用[] \\\\ []转义[] -括号

$("#"+tabName+"Name\\["+id+"\\]")

From jQuery Docu: LINK 从jQuery Docu: 链接

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\\\. 要将任何元字符(例如!“#$%&'()* +,。/ :; <=>?@ [] ^`{|}〜)用作名称的文字部分,必须以两个反斜杠转义:\\\\。

I think that you can't put [ and ] in the id attribute. 我认为您不能将[]放在id属性中。

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods ("."). ID和NAME令牌必须以字母([A-Za-z])开头,后跟任意数量的字母,数字([0-9]),连字符(“-”),下划线(“ _”) ,冒号(“:”)和句点(“。”)。

See this answer: https://stackoverflow.com/a/79022/863110 看到这个答案: https : //stackoverflow.com/a/79022/863110

Try to replace it to underscore ( _ ) for example: 尝试将其替换为下划线( _ ),例如:

<input type="hidden" id="toursImageName_0" />

您应该将toursImageName[0]更改为toursImageName0

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

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