无法使用jQuery获取attr ID [英] Can't get attr id with jquery
问题描述
我正在使用 http://www.symfocal.com/的symfony应用程序(2.3)
I'm on a symfony application (2.3) using http://www.symfocal.com/
我尝试更改日历的显示方式,但是我在使用jquery时遇到了一些麻烦:
I try to change the way the calendar is diplayed but I have some trouble with jquery :
这是修改后的代码中与我的问题有关的部分.
Here's the part of the modified code concerning my problem.
带有jquery的树枝文件:
The twig file with jquery :
<script>
...
jQuery(document).ready(function() {
var cal1 = $('#cal1');
var cal2 = $('#cal2');
function fillCalendar(mois, annee, cal)
{
$(cal).css('display', 'none');
$(cal).find('a').removeClass().unbind();
{% if admin == false %}
$(cal).find('a').click(function(e){e.preventDefault();});
{% endif %}
...
var id = $(cal).attr('id');
$.post(url,{
month: mois,
year: annee,
},function(data){
if(data.responseCode==200 )
{
var days_previous_month=data.days_previous_month;
var nb_days_prev=data.nb_days_prev;
$('#'+id+'_title').html(data.current_month);
var count=1;
while(count<43)
{
{% if admin %}
if(data.calendar[(count-1)].dates!="")
{
$('#'+id+'_cell_'+count).attr("title", data.calendar[(count-1)].dates).click(changeState);
}
else
{
$('#'+id+'_cell_'+count).click(function(e){e.preventDefault();});
}
{% endif %}
if(data.calendar[(count-1)].booked==-1) $('#'+id+'_cell_'+count).html(days_previous_month - nb_days_prev + count);
else $('#'+id+'_cell_'+count).html(data.calendar[(count-1)].fill);
if(data.calendar[(count-1)].booked==1) $('#'+id+'_cell_'+count).addClass(data.calendar[(count-1)].classe);
else if(data.calendar[(count-1)].booked<0) $('#'+id+'_cell_'+count).addClass("dates_preview");
count++;
}
$(cal).css('display', 'block');
}
else
...
function changeMonths(step)
{
gap = gap + step;
var newMonth = month + (1 * gap)%12;
var gapYear = (1 * gap)/12;
var newYear = year + gapYear - gapYear%1;
if (newMonth<=0)
{
newMonth+=12;
}
{% for item in items %}
fillCalendar(newMonth-12*Math.floor((newMonth)/13), newYear+Math.floor((newMonth)/13), 'cal{{ item.id }}' );
{% endfor %}
}
...
$(window).load(function(){
changeMonths(0);
});
});
</script>
和html部分-带有细枝包括:
and the html part - with a twig include :
<div class="calendar_wrapper">
<div id="cal1" class="calendar">
<table class="tab_calendar" align="center">
<tr>
<td class="title_calendar" colspan="43" width="100%">
<span id="cal1_title"></span>
</td>
</tr>
<tr>
<td class="tech" >
<span>Technicien</span>
</td>
{% for i in 1..6 %}
<td class="cell_jour" >
<span>Lun</span>
</td>
<td class="cell_jour" >
<span>Mar</span>
</td>
<td class="cell_jour" >
<span>Mer</span>
</td>
<td class="cell_jour" >
<span>Jeu</span>
</td>
<td class="cell_jour" >
<span>Ven</span>
</td>
<td class="cell_jour" >
<span>Sam</span>
</td>
<td class="cell_jour" >
<span>Dim</span>
</td>
{% endfor %}
</tr>
{% for item in items %}
<tr>
<td class="{{ item.id }}">{{ item.name }}</td>
{% for i in 1..42 %}
<td class="cell_calendar"><a id="cal{{ item.id }}_cell_{{ i }}" href="#"></a></td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
</div>
我对正在发生的事情的理解:
My understanding of what is happening :
$(window).load(函数费用changeMonths(0)
$(window).load(function charges changeMonths(0)
changeMonths为每个项目调用函数fillCalendar(目前用于测试有两个项目(ID为1和ID 2)
changeMonths calls the function fillCalendar for each item (at this time for testing there are two items (with id 1 and id 2)
这是我出错的地方:
说明:
var id = $(cal).attr('id');
未获取ID-未定义
但是作为参数接收的变量cal是cal1&.然后是cal2,并应返回id,表示cal1或cal2.
But the variable cal received as parameter is cal1 & then cal2 and should return the id, meaning cal1 or cal2.
有人可以帮我解决这个问题吗?
Could someone help me to resolve this problem ?
我遵循了ihsan的答案-现在更好了,但是只有第一个日历行填满了一个月的天数
EDIT : I followed the answer of ihsan - its better now but only the first calendar line fills up with month days
console.log(cal); shows now :
#cal1
#cal2
好的,我也必须修改我的html才能使cal2正常工作-感谢您的帮助
EDIT 2 : Ok I had to modify my html to make cal2 work too - thanks for your help
推荐答案
html部分:
...
<td id="cal{{ item.id }}">{{ item.name }}</td>
js部分:
...
fillCalendar(newMonth-12*Math.floor((newMonth)/13), newYear+Math.floor((newMonth)/13), '#cal{{ item.id }}' );
这篇关于无法使用jQuery获取attr ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!