0
这个问题是后续的一个this之一。我创建了一个simple example来检查代码在处理程序中的执行方式。对于形式调试jquery处理程序
<form id="calendar_id" method="post">
Insert date: <input id="date_id" type="text" name="l_date" required>
</form>
我试图检索使用下面的JavaScript中的字段:
function get_form_data_uid($form) {
var unindexed_array = $form.serializeArray();
var indexed_array = {};
$.map(unindexed_array, function (n, i) {
indexed_array[n['name']] = n['value'];
});
indexed_array['uid'] = 'badbfadbbfi';
return indexed_array;
}
$("#calendar_id").submit(function (e) {
var uri, method, formId, $form, form_data;
// Prevent default submit
e.preventDefault();
e.stopImmediatePropagation();
uri = "/";
method = "POST";
formId = "#calendar_id";
$form = $(formId);
form_data = get_form_data_uid($form);
alert("form_data " + form_data);
// Set-up ajax call
var request = {
url: uri,
type: method,
contentType: "application/json",
accepts: "application/json",
cache: false,
// Setting async to false to give enough time to initialize the local storage with the "token" key
async: false,
dataType: "json",
data: form_data
};
// Make the request
$.ajax(request).done(function (data) { // Handle the response
// Attributes are retrieved as object.attribute_name
console.log("Data from change password from server: " + data);
alert(data.message);
}).fail(function (jqXHR, textStatus, errorThrown) { // Handle failure
console.log(JSON.stringify(jqXHR));
console.log("AJAX error on changing password: " + textStatus + ' : ' + errorThrown);
});
});
然而,处理程序中的代码不执行(不显示的警报)。为什么?
编辑:
代码工作jsfiddle但不是在Firefox浏览器。
我在复制代码时犯了一个错误。警报不显示,无论。 – Sebi
好的,事实上,即使在我修复函数名后,你的小提琴似乎也会失败并出现错误。但是,当我然后将更正的代码逐字复制到此Codepen http://codepen.io/anon/pen/BodXRp时,它确实会按预期显示警报。 – Thernys
噢,你的小提琴(修复后)只会失败,因为你没有将jQuery作为一个库加入,而是在你的代码中使用它。这两个修复后,小提琴确实显示警报。 (https://jsfiddle.net/8g0rvgnt/7/) – Thernys