Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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获取图像的索引值?_Jquery_Image - Fatal编程技术网

如何使用jquery获取图像的索引值?

如何使用jquery获取图像的索引值?,jquery,image,Jquery,Image,大家好,我正在处理jquery,我需要避免添加多个图像,我在这里编写了代码,但是 我总是得到图像的索引值是-1,并且总是若条件触发,我会出错 你能帮我做那件事吗?提前谢谢 这是第一个div的代码 <div><span class="span1"> <img width="30" hieght="30" name="productimage" src="http://panther:805/Computers.jpg"></span> <spa

大家好,我正在处理jquery,我需要避免添加多个图像,我在这里编写了代码,但是

我总是得到图像的索引值是-1,并且总是若条件触发,我会出错 你能帮我做那件事吗?提前谢谢

这是第一个div的代码

<div><span class="span1">
<img width="30" hieght="30" name="productimage" src="http://panther:805/Computers.jpg"></span>
<span class="span1">Black_xxl_Slim</span>
<span class="span1">1</span>
<a class="pull-right" href="#"><i onclick="Add(this)" class="icon-plus"></i></a>
</div> 

黑色xxl修身
1.
下面是第二个div的代码:

<div class="span6" id="separat">
            <ul class="thumbnails pre-scrollable" id="productbundles">
            </ul>
        </div>

下面是我的代码

  function Add(obj) {
           var addedImagesArray = [];
           var img = $(obj).closest('div').find('img').first();

         var image_src = $(img).attr('src');
        var newobj = $('<li class="span2" id="bunle' + id++ + '"><a href="javascript: void(0)"><img hieght=30  width=30 src="' + image_src + '" /></a><h5>' + "Name:" + name + '</h5><span id="pric' + id + '"><b>' + "Price:" + price + '</b></span>');
         if   (addedImagesArray.indexOf(image_src)) {
           alert("Image Already Added");//src present in the array so do nothing or show an alert..
         } else {
           $('#productbundles').append(newobj); //append the image
          addedImagesArray.push(image_src); ///add src to array
         }
      }
功能添加(obj){
var addedImagesArray=[];
var img=$(obj).closest('div').find('img').first();
var image_src=$(img.attr('src');
var newobj=$('
  • “++”名称:“+Name++++”价格:“+Price++”); if(addedImagesArray.indexOf(image_src)){ 警报(“已添加图像”);//数组中存在src,因此不执行任何操作或显示警报。。 }否则{ $('#productbundles').append(newobj);//追加图像 addedImagesArray.push(image_src);///将src添加到数组 } }
  • 在上面我有两个分区,我从一个分区添加到另一个分区的图像

    但问题是我总是得到-1索引值,请帮助我做到这一点,提前感谢

    您需要将的返回值与
    -1
    进行比较,并在函数外部声明全局范围内的AddDimagesarray

    改变

    if(addedImagesArray.indexOf(image_src)) {
    


    在全局范围内声明AddDimagesArray

    您需要将的返回值与
    -1
    进行比较,还需要在函数外部的全局范围内声明addedImagesArray

    改变

    if(addedImagesArray.indexOf(image_src)) {
    


    在全局范围内声明AddDimagesArray

    更改:

    if(addedImagesArray.indexOf(image_src)) {
      alert("Image Already Added");
    

    更改:

    if(addedImagesArray.indexOf(image_src)) {
      alert("Image Already Added");
    


    快捷方式:
    如果(~arr.indexOf(x))
    对于跨浏览器方法,请使用jquery方法$.inArray(),它可以使用adil,但不能防止添加重复图像我需要避免多次添加相同的图像在函数外部的全局范围内声明addedImagesArray,我已经更新了我的答案Thank adil它正在工作感谢我非常感谢jazakallahshortcut的2:
    如果(~arr.indexOf(x))
    对于跨浏览器方法,请使用jquery方法$.inArray()它在adil中工作,但不能防止添加重复图像我需要避免多次添加相同的图像声明addedImagesArray在全局范围外的函数中,我已经更新了我的回答谢谢adil它正在工作谢谢我非常感谢jazakallahthanks在2之前为我的回复sudhir它也在工作但我需要限制多次添加相同的图像你能帮我吗谢谢我的回复sudhir它也在工作但我需要限制多次添加相同的图像你能帮我吗
    if(addedImagesArray.indexOf(image_src) > -1 ) {
      alert("Image Already Added");