预期的分号阿贾克斯 - Jquery



所以我有一个ajax请求,它给了我一个"预期;">错误。

$(document).ready(function () {
$("#btnSubmit").bind("click", function () {
var ids[] = null;
var dates[] = null;
createUpdateArrays();
var url = "/hello/index/";
$.ajax({
type: "Post",
url: url,
data: { ids: ids, dates: dates },
async: false,
contentType: "application/json;charset=utf-8",
success: function (success) {
if (success === true) {
alert("HERE WE ARE");
}
else {
alert("eror")
}
},
error: function (e) {
alert("eror")
}
});
});
createUpdateArrays() {  // Expected ; <---- here
$('.id').each(function(i) {
var rid = $(this).id;
$('.planned-date').each(function(x) {
if (i===x) {
var date = $(this).text;
ids.push(rid);
dates.push(date);
}
});
});
}; // <--- this is where the createUpdateArrays function ends. 
});

我指出了代码中发生错误的位置。我不明白它是什么,我觉得我在语法上是正确的。我将分号放在函数结束的位置。如果有人能帮忙,那就太好了。我觉得这是一个小错误,或者我错过了一些东西。谢谢。

您需要将 creatUpdateArrays 声明为一个函数。

$(document).ready(function () {
$("#btnSubmit").bind("click", function () {
var ids[] = null;
var dates[] = null;
createUpdateArrays();
var url = "/hello/index/";
$.ajax({
type: "Post",
url: url,
data: { ids: ids, dates: dates },
async: false,
contentType: "application/json;charset=utf-8",
success: function (success) {
if (success === true) {
alert("HERE WE ARE");
}
else {
alert("eror")
}
},
error: function (e) {
alert("eror")
}
});
});
function createUpdateArrays() {  // Expected ; <---- here
$('.remedy-id').each(function (i) {
var rid = $(this).id;
$('.planned-date').each(function (x) {
if (i === x) {
var date = $(this).text;
ids.push(rid);
dates.push(date);
}
});
});
};  
});

或者甚至在文档外部声明函数 ready,只需在 ready 中引用该函数。

$(document).ready(function () {
$("#btnSubmit").bind("click", function () {
var ids[] = null;
var dates[] = null;
createUpdateArrays();
var url = "/hello/index/";
$.ajax({
type: "Post",
url: url,
data: { ids: ids, dates: dates },
async: false,
contentType: "application/json;charset=utf-8",
success: function (success) {
if (success === true) {
alert("HERE WE ARE");
}
else {
alert("eror")
}
},
error: function (e) {
alert("eror")
}
});
});
});
function createUpdateArrays() {  // Expected ; <---- here
$('.remedy-id').each(function(i) {
var rid = $(this).id;
$('.planned-date').each(function(x) {
if (i===x) {
var date = $(this).text;
ids.push(rid);
dates.push(date);
}
});
});
}; // <--- this is where the createUpdateArrays function ends. 

试试这个。

这是因为您缺少"函数"关键字,您应该使用它来定义函数。

您需要为createUpdateArrays()添加function并在$(document).ready()之外定义它

function createUpdateArrays() {  // Expected ; <---- here
$('.remedy-id').each(function(i) {
var rid = $(this).id;
$('.planned-date').each(function(x) {
if (i===x) {
var date = $(this).text;
ids.push(rid);
dates.push(date);
}
});
});
}; // <--

你已经不能从createUpdatesArray函数推送任何内容到ids和dates。因为 id 和日期未定义为全局变量。因此,您的 post 函数将始终发送 null。你需要稍微改变你的代码。 您可以定义 id 和日期,例如:

var ids = {};
var dates = {};

请记住,现在您的函数和"ids,日期"不在同一范围内。 所以你不能从createUpdatesArray函数推送任何东西。 必须在<script>document.ready函数之间定义此项。

您的最后一个代码将如下所示:

var ids = {}; // ids is global now.
var dates = {}; // dates is global now.
$(document).ready(function () {
$("#btnSubmit").bind("click", function () {
//var ids[] = null;
//var dates[] = null;
createUpdateArrays();
var url = "/hello/index/";
$.ajax({
type: "Post",
url: url,
data: { ids: ids, dates: dates },
async: false,
contentType: "application/json;charset=utf-8",
success: function (success) {
if (success === true) {
alert("HERE WE ARE");
}
else {
alert("eror");
}
},
error: function (e) {
alert("eror");
}
});
});
});
function createUpdateArrays() {  // Expected ; <---- here
$('.remedy-id').each(function(i) {
var rid = $(this).id;
$('.planned-date').each(function(x) {
if (i===x) {
var date = $(this).text;
ids.push(rid);
dates.push(date);
}
});
});
}; // <--- this is where the createUpdateArrays function ends. 

最新更新