function manageVoting() {
$("div.votemaincontainer").each(function () {
// variable Declaration and intialization.
var myRating = parseInt($(this).find('#[id$=hfMyVote]').val());
var currentVote = parseInt($(this).find('#[id$=hfCurrentVote]').val());
$('.VoteDownImage').live(click, function () {
ajaxcall(0);
});
$('.voteupImage').live(click, function () {
ajaxcall(1);
});
}
function ajaxcall(a){ // here it's giving error.
var questionID = parseInt($(this).find('#[id$=currentquestionId]').val());
var objectType = 100;
var previousvote = 2;
$.ajax({
url: 'UserControls/Vote/VoteAction.aspx/Voting',
cache: false,
type: 'POST',
data: '{ObjectType: "' + objectType + '", guid: "' + currentGuid + '",previousVote: "' + previousvote + '",vote:"' + a + '"}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data) {
var result = eval(data.d);
}
});
}
$(function () {
// manageVoting();
});
答案 0 :(得分:4)
编辑:在改进了问题中的代码缩进后,似乎您忘记在{{1}的结束括号之前用.each()
关闭});
循环1}}功能。
正确的代码缩进非常有用。
此外,在循环中应用manageVoting()
没有多大意义。你只需要这样做一次。
以下是具有改进功能的代码:
.live()
原始回答:
您没有在javascript中为函数参数(或任何其他变量)指定类型。这是因为键入松散,任何变量都可用于存储任何类型。
这一行:
// Call these once on page load.
// Note the difference in capitalization in your selectors. Make sure it is
// the same in your HTML classes
$('.VoteDownImage').live(click, function () {
ajaxcall(0);
});
$('.voteupImage').live(click, function () {
ajaxcall(1);
});
function manageVoting() {
$("div.votemaincontainer").each(function () {
// variable Declaration and intialization.
var myRating = parseInt($(this).find('#[id$=hfMyVote]').val());
var currentVote = parseInt($(this).find('#[id$=hfCurrentVote]').val());
}); // closed the .each() loop
}
function ajaxcall(a){ // here it's giving error.
var questionID = parseInt($(this).find('#[id$=currentquestionId]').val());
var objectType = 100;
var previousvote = 2;
$.ajax({
url: 'UserControls/Vote/VoteAction.aspx/Voting',
cache: false,
type: 'POST',
data: '{ObjectType: "' + objectType + '", guid: "' + currentGuid + '",previousVote: "' + previousvote + '",vote:"' + a + '"}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data) {
var result = eval(data.d);
}
});
}
$(function () {
// manageVoting();
});
应该是:
function ajaxcall(int a)
答案 1 :(得分:1)
@Nishant - 当您完成解开代码时,会遇到很多问题。
通过查看代码,您尝试对图像进行上/下投票。
我有一些问题和陈述:
manageVoting
中用于?this
中的parseInt($(this).find('#[id$=currentquestionId]').val())
引用并非您在获取ID时所需的内容。ajaxCall
以外的其他内容。以下是您的代码的开头:
function updateVote(elementClicking, vote){ // here it's giving error.
var imagVote = {
ObjectType: 100,
guid: parseInt($(elementClicking).find('#[id$=currentquestionId]').val()),
previousVote: 2,
vote: vote
} ;
$.ajax({
url: 'UserControls/Vote/VoteAction.aspx/Voting',
cache: false,
type: 'POST',
data: imagVote,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data) {
var result = eval(data.d);
}
});
}
$('.VoteDownImage').live(click, function () {
updateVote(this, 0);
});
$('.voteupImage').live(click, function () {
updateVote(this, 1);
});
function manageVoting() {
$("div.votemaincontainer").each(function () {
// variable Declaration and intialization.
var myRating = parseInt($(this).find('#[id$=hfMyVote]').val());
var currentVote = parseInt($(this).find('#[id$=hfCurrentVote]').val());
});
}
$(function () {
manageVoting();
});
最后一条不受欢迎的建议。 不要让代码更复杂 ##标题##