Jquery ajax调用在firefox中无法执行
jquery ajax call not executing in firefox
这个问题是这个问题和这个问题的后续问题。我无法通过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;
}
onsubmit处理程序不会被执行,即使dom ready事件在这个问题中被使用。
我怎么能提交字段只有一次使用ajax api从jquery?
编辑:jsplay前一个问题的例子。尽管代码在jsfiddle上运行,但在firefox中运行时却失败了。
像这样使用on
事件处理程序:
$(document).on("submit","#changepassword_form_id",function(e){
...code here...
});
这个委托,因为#changepassword_form_id
还没有在document.ready
上定义。
既然,您正在使用required
属性的输入和需要检查填写的表单,您可以使用submit
事件。
你显然是在div加载之前添加了html dom
.
这是流的一步一步分解…
- 您正在加载
<head>
中的.js文件。 -
您正在调用文件内部的函数,该函数具有以下行,
document.getElementById("main").innerHTML = changePassword;
但是,但是没有id为"main"的东西!! -
然后
<body>
正在加载,其中包含id为"main"的div。
不是多管闲事,但这个问题是你的代码设计错误的结果,用任何解决方法来修复它只会让你更加头疼,所以我强烈建议你更新代码设计,除非你对代码几乎没有控制,在这种情况下,补丁工作是你唯一的选择。
如果你是通过拼凑,你总是可以使用.off.on
事件委托,它消除了代码重新运行时事件重复的可能性。
相关文章:
- 未为youtube执行Firefox Addon Pagemod
- FireFox扩展对TinyMCE编辑器执行脚本注入
- 未在Firefox中执行PageMethod的返回函数
- Firefox 在应该执行的时候不执行 JavaScript
- 使用webcomponenetsjs的HTMLImports以意外的执行顺序加载导入-firefox
- 使javascript在firefox中的插件javascript之后执行
- Firefox中未执行JavaScript
- Javascript函数无法在safari,Firefox和chrome中执行
- Firefox 引导插件:安装事件未执行
- 通过 javascript 或 java 在 Firefox 开发者工具栏上传递和执行命令
- Java脚本(根本没有)被I.E.或Firefox执行,但Chrome没有问题
- 为什么 JavaScript 执行会影响 Firefox 中的 css 动画和动画 gif
- Firefox SDK 多次执行请求
- Firefox 上的 Javascript 在 onchange 任务上意外停止执行
- JavaScript 未在 Firefox 中执行.铬还行
- HTML 模板和 HTML 导入 - 内部脚本未在 Firefox 中执行
- 在 JavaScript 中按特定键执行某些操作在 Chrome 中有效,但在 Firefox 中不起作用
- eval_in_page javascript执行在firefox中有效,但在phantomjs中无效
- 要显示此页面,Firefox必须发送重复先前执行的任何操作(如搜索或订单确认)的信息
- Firefox控制台错误:试图在清除的范围内运行编译并执行脚本