2015-11-04 87 views
0

我在我的jQuery有问题。有人可以帮我修复它吗?我有我的代码上的jsfiddle这里:更改对象值jquery

http://jsfiddle.net/tbk5tuj9/

我的问题是,我想改变image source_image_widthheight到其他大小。我的代码有可能吗?

这是我的jQuery:

$(function() { 
    var value = {}; 

    value = { 
     "zoom_element": '#zoom', 
     "magnifier_opacity": 1, 
     "source_image_width": 200, 
     "source_image_height": 200, 
    }; 

    $(".klik").click(function() { 
     console.log("Klik"); 
     value = { 
      zoom_element: '#zoom', 
      magnifier_opacity: 1, 
      source_image_width: 2, 
      source_image_height: 100, 
     }; 
    }); 

    $('#etalage').etalage(value); 

    console.log(value); 
}); 
+1

**可能**重复[*如何从异步调用返回响应* *](http://stackoverflow.com/questions/14220321/how-to-return-the-response-从一个异步呼叫),但我会把它留给其他人来打电话(如果我投了,Mjolnir会踢)。 –

+0

我明白,你想要的是:http://jsfiddle.net/tbk5tuj9/9/ ??? –

+0

感谢评论A.沃尔夫,但是当我点击“klik”,它再次使一些图片先生@ A.Wolff – arickanjass

回答

1

您的代码完全替换value对象,成功。唯一的(真正的)问题是您的console.log外部您的click处理程序。如果您想在点击后看到value,那么您需要将该转换为您的点击处理程序的


小问题是,你在这里创建一个对象:

var value = {}; 

...你再扔掉完全和替换这里:

value = { 
    "zoom_element": '#zoom', 
     "magnifier_opacity": 1, 
     "source_image_width": 200, 
     "source_image_height": 200, 
}; 

那些可以合并,或者将= {}关闭012xx一行。


另外,如果你只是想修改你的对象,你可以分配给它的属性,如:

value.magnifier_opacity = 1; 

的例子,只是移动的console.log,但仍取代整个对象:

$(function() { 
 
    var value = {}; 
 

 
    value = { 
 
    "zoom_element": '#zoom', 
 
    "magnifier_opacity": 1, 
 
    "source_image_width": 200, 
 
    "source_image_height": 200, 
 
    }; 
 

 
    $(".klik").click(function() { 
 
    snippet.log("Klik"); 
 
    value = { 
 
     zoom_element: '#zoom', 
 
     magnifier_opacity: 1, 
 
     source_image_width: 2, 
 
     source_image_height: 100, 
 
    }; 
 
    snippet.log(JSON.stringify(value)); // <========= Moved and 
 
             // adjusted for demo 
 
    }); 
 

 
    // $('#etalage').etalage(value); Commented out for demo 
 
});
<input type="button" class="klik" value="Click me"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> 
 
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

+0

感谢您的答案和解决方案先生。我尝试它,但仍然没有改变我的源图像的宽度和高度。我的问题是当我点击,它改变源图像的宽度和高度,并重新加载$('#etalage')与新对象 – arickanjass