【发布时间】:2016-01-12 21:22:32
【问题描述】:
此问题是对this 问题和this 问题的后续问题。我无法通过 jquery 的 ajax api 发送表单字段值。代码如下:
index.html
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<link rel="stylesheet" type="text/css" href="index.css">
<script src="https://code.jquery.com/jquery-2.1.3.js"></script>
<script type="text/javascript" src="index.js"></script>
</head>
<body onload="welcome()">
<div class id="main"></div>
</body>
</html>
欢迎功能在 index.js 中实现:
index.js
function welcome()
{
view_account();
}
function get_form_data_with_token($form){
var unindexed_array = $form.serializeArray();
var indexed_array = {};
$.map(unindexed_array, function(n, i){
indexed_array[n['name']] = n['value'];
});
indexed_array['token'] = 'adafdafdgfdag';
return indexed_array;
}
$(document).ready(function(){
$("#changepassword_form_id").submit(function(e){
var uri, method, formId, $form, form_data;
// Prevent default jquery submit
e.preventDefault();
e.stopImmediatePropagation();
uri = location.protocol + '//' + location.host + "/change_password";
method = "POST";
formId = "#changepassword_form_id";
$form = $(formId);
form_data = get_form_data_with_token($form);
alert("form_data: token = " + form_data['token'] + " password3 = " + form_data['l_password3'] + " password4 = " + form_data['l_password4']);
// 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);
}
);
});
});
function view_account()
{
var changePassword;
changePassword = "<form action=\"/change_password\" id=\"changepassword_form_id\" method=\"post\">";
changePassword = changePassword + "<br><label>Old Password: </label><input id=\"password3\" type=\"password\" name=\"l_password3\" required><br>";
changePassword = changePassword + "<br><label>New Password: </label><input id=\"password4\" type=\"password\" name=\"l_password4\" required><br><br>";
changePassword = changePassword + "<input type=\"submit\" value=\"Change Password\">";
changePassword = changePassword + "</form>";
// Replace the original html
document.getElementById("main").innerHTML = changePassword;
}
即使使用了this 问题中提到的dom 就绪事件,onsubmit 处理程序也不会执行。
如何使用 jquery 的 ajax api 只提交一次字段?
编辑:
jsfiddle 上一个问题的示例。即使代码在 jsfiddle 上运行,它在火狐中运行时也会失败。
【问题讨论】:
-
你能附上一个jsfiddle吗?
-
它来自上一个问题,只有一个输入字段 jsfiddle.net/8g0rvgnt/7 。它适用于 jsfiddle,但不适用于 firefox。
-
如果每次都显示,为什么要创建用于使用 JavaScript 更改密码的表单?只需将其放入标记中即可。
-
这是一个简化的例子。在实际应用中不是每次都显示。
-
您正在将一个事件附加到一个不存在的元素。加载前准备运行。
标签: javascript jquery html ajax forms