我在使用Uncaught error unexpected token (
循环的字符串script
标记中收到错误for
<script type="text/javascript">
var offer_foto_index = 'slide_offer_preview_1';
var slide_offer_preview_ = [];
var photo_array = <%= @photos.to_json.html_safe %>;
var offer_foto = {
for(var i = 0; i < <%= @photos.length %>; i++) {
slide_offer_preview_[i] = photo_array[i];
}
}
</script>
我需要一个offer_photo
变量,该变量应包含数组slide_offer_preview_1
,slide_offer_preview_2
,slide_offer_preview_3
等。
我在哪里弄错了?谢谢!
答案 0 :(得分:1)
如果没有看到您的实际渲染代码是什么样的,这将很难。但我的猜测是你应该使用photo_array
代替@photos
:
<script type="text/javascript">
var offer_foto_index = 'slide_offer_preview_1';
var slide_offer_preview_ = [];
var photo_array = <%= @photos.to_json.html_safe %>;
//var offer_foto = { - I've commented this because you don't need it...
for(var i = 0; i < photo_array.length; i++) {
slide_offer_preview_[i] = photo_array[i];
}
//}
</script>
答案 1 :(得分:1)
根据你的评论回答:“......我需要一个offer_photo变量。它应该包含像slide_offer_preview_1,slide_offer_preview_2等数组。”
<script type="text/javascript">
var offer_foto_index = 'slide_offer_preview_1';
var slide_offer_preview_ = [];
var photo_array = <%= @photos.to_json.html_safe %>;
var offer_foto = {};
for(var i = 0; i < <%= @photos.length %>; i++) {
offer_photo['slide_offer_preview_' + i] = photo_array[i];
}
</script>
或者如果你需要它在“offer_foto”对象中的一个数组中:
var offer_foto = {};
offer_foto.slide_offer_preview_ = [];
for(var i = 0; i < <%= @photos.length %>; i++) {
offer_photo.slide_offer_preview_[0] = photo_array[i];
}