我想改變一個全局jQuery變量在foreach循環內每次有一個新的模型項目的值。我想添加新的日期到日曆,但不能這樣做,直到我可以從foreach循環訪問這些函數。引用全局jQuery函數變量在foreach循環
*編輯***
我過我的簡化,例如用於這是由VOV v正確地回答了這個問題,實際的代碼會做很多不止添加值,因爲它會增加數據到日曆。我已經添加了更多的代碼,以顯示它會做的更好一點
的jQuery:
<div id="calendar" style="width: 500px;" />
<script type="text/javascript">
$(document).ready(function() {
var calendar = $('#calendar').glDatePicker(
{
showAlways: true,
borderSize: 0,
dowOffset: 1,
selectableDOW: [1, 2, 3, 4, 5],
selectableYears: [2012, 2013, 2014, 2015],
specialDates: [
{
date: new Date(2013, 0, 8),
data: { message: 'Meeting every day 8 of the month' },
repeatMonth: true
},
{
date: new Date(2013, 5, 7),
data: { message: 'Meeting every day 8 of the month' }
},
],
onClick: function (target, cell, date, data) {
target.val(date.getFullYear() + ' - ' +
date.getMonth() + ' - ' +
date.getDate());
if (data != null) {
alert(data.message + '\n' + date);
}
}
}).glDatePicker(true);
$('#visible').change(function() {
var showAlways = eval($(this).val());
calendar.options.showAlways = showAlways;
if (!showAlways) {
calendar.hide();
}
else {
calendar.show();
}
});
});
var value = 0;
$('#total').click(function() {
alert(value);
});
function add() {
// will eventually add new specialDates to the calendar taken from model items
//test lines
//value = value + 1;
//return value;
}
</script>
Razor視圖:
<input type="button" id="total" />
@foreach (var item in Model){
if (item.AppointmentStatus == "active")
{
// item display code
@: <script type="text/javascript"> add();</script>
}
if (item.AppointmentStatus == "history")
{
// item display code
}
}
我運行此,並得到如下錯誤因爲它沒有看到其他代碼
'0x800a1391 - JavaScript運行時錯誤:'add'是unde罰款'
感謝您的快速響應。我已經嘗試了它進出document.ready,並調整了這個問題以外的代碼,因爲只有在它內部移動它才能測試 – ConfusedShark 2013-05-01 10:57:27