Добрый вечер, возникла еще проблемка. Возился часа 4 - так и не решил.
Через цикл в шаблон вывожу items и все данные относящиеся к ним.
Все ок генерируется, но вот скрипты работают только для первого блока!!
Не могу понять почему, и выборка по селкторам ок вроде.
Вью в которой шлется ajax - ок работает для первого item.
Отлично если подскажите.
Не работает скрипт уже со второго блога при генирации.
<a href="#" class="button-general" id="subscribe-but" >Подписаться</a>
Вот код шаблона.
{% for item in items %}
<article id="{{ item.id }}">
<div class="user-info-general">
<a href="#1" class="user-link"><img src="/media/img/ot.jpg"/>
<div class="user-info-general-section">
{% if item.name %}
<p>{{ item.user.username }}<br/>
<span>{{ item.name }}</span>
</p>
{% else %}
<p class="one-name">{{ item.user.username }}</p>
{% endif %}
</a>
</div>
{% if user.id %}
<a href="#" class="button-general" id="subscribe-but" >Подписаться</a>
<input type="hidden" value="{{ item.user.id }}" id="subscribe-hid" />
{% else %}
<a href="/auth/login/" class="button-general">Подписаться</a>
{% endif %}
</div>
{% for photo in item.get_photos %}
<div class="photo-block">
{% for tag in photo.get_tags %}
{% if tag.z_position == 1 %}
<div class="mark-brand" style="top:{{ tag.y_position }}; left:{{ tag.x_position }};" id="{{ tag.id }}">
<svg class="star-mark star-green-active" viewBox="0 0 105 105">
<use xlink:href="#star-mark"></use>
</svg>
</div>
<div class="brand-panel active" id="" style="z-index:999;">
{% if tag.url == "" %}
<a>
<p>{{ tag.brand_name }}</p>
</a>
{% else %}
<a href="{{ tag.url }}">
<p>{{ tag.brand_name }}</p>
<div class="button-mark" >
<svg class="arrow-right-icon arrow-right-icon-green" viewBox="0 0 15.15 22">
<use id="mark-close-use" xlink:href="#arrow-right-icon"></use>
</svg>
</div>
</a>
{% endif %}
</div>
{% else %}
<div class="mark-brand" style="top:{{ tag.y_position }}; left:{{ tag.x_position }};" id="{{ tag.id }}">
<svg class="star-mark" viewBox="0 0 105 105">
<use xlink:href="#star-mark"></use>
</svg>
</div>
<div class="brand-panel position" id="hide">
{% if tag.url == "" %}
<a>
<p>{{ tag.brand_name }}</p>
</a>
{% else %}
<a href="{{ tag.url }}">
<p>{{ tag.brand_name }}</p>
<div class="button-mark">
<svg class="arrow-right-icon arrow-right-icon-green" viewBox="0 0 15.15 22">
<use id="mark-close-use" xlink:href="#arrow-right-icon"></use>
</svg>
</div>
</a>
{% endif %}
</div>
{% endif %}
{% endfor%}
<img src="/uploads/{{ photo.image }}" id="image"/>
{% if group == 'partner' and item.user.id == user.id %}
<a href="/add/{{ item.id }}" class="button-add-edit" id="button-add-edit"></a>
{% elif group == 'moderator' %}
<a href="/add/{{ item.id }}" class="button-add-edit" id="button-add-edit"></a>
{% endif %}
</div>
{% endfor%}
</article>
{% endfor %}
Вот код скрипта (реализация подписки)
// JavaScript Document
$(document).ready(function(){
$("#subscribe-but").click(function() {
var this_sub_button = $(this);
var user_to_sub = $(this).next("input").attr("value");
$.ajax({
url: "/subscribe/",
type: "POST",
dataType:"html",
data: {
"user_to_sub": user_to_sub
},
error: function() {
alert('Ошибка получения запроса');
},
success: function(data) {
if (data == "sub"){
this_sub_button.text('Отписаться').attr("class","button-active2");
}
else{
this_sub_button.text('Подписаться').attr("class","button-general");
}
}
});
});
});
Пишу всего два месяца, поэтому не осуждайте))
Может проблема в подгрузке скриптов. ...
Спасибо.