jquery.one()触发了一次查询,但是如果.one(()内部出现问题,我怎么能触发一次查询呢
jquery .one() fire query for one time but how can i able to fire query once if something is going wrong inside .one()?
我有一个登录页面,即使用户多次点击,我也想提交一次凭据。这就是为什么我想使用.one()
,但是.one()有一个问题,假设用户有一些凭据问题,那么下次他修复它并尝试再次发送时。one()将不起作用。然后你必须做一些步骤。如何解决这个问题?
$(document).ready(function(){
$("#submit").one("click", function() {
data = some data
if data !==""{
$.ajax({
url: '/my url/',
type: 'POST',
contentType: 'application/json',
dataType: 'json',
data :data,
processData: false,
success: function(data){
alert("done")
},
error: function(jqXHR, textStatus, errorThrown){
alert("Some Error")
//here i want to do something so that if user click the submit button it will fire once
}
})
}
else
//data is not there
//here i want to do something so that if user click the submit button it will fire once
});
您可以再次绑定处理程序:
$("#submit").one("click", function handler() { // give the function a name
// keep a reference to the clicked element so that you can access it
// inside callbacks
var self = this;
// ...
// when something went wrong, bind the handler again
$(self).one('click', handler);
});
您希望使用one()
来实现这一点,但使用简单的ajax。与现在的方法不同,创建一个名为"allowAjaxCheckLogin"的变量,并将其设置为true。在您的常规点击处理程序中,您可以检查它是否为true,如果是,请将其设置为false,然后执行ajax部分。当ajax完成并且用户需要能够再次执行时,将其设置回真正的
var allowAjaxCheckLogin = true; // set to true, normal/startsituation allows a click
$('#submit').on('click', function(){
// User may trigger actions:
if( allowAjaxCheckLogin === true){
allowAjaxCheckLogin = false; // Instantly set to false, if the user clicks again, nothing will happen
$.ajax({ /* ajax settings here */ })
.complete(function(){
// this always fires, here you can check if you need to refresh the page to anochter location
allowAjaxCheckLogin = true; // set back so the user may trigger again
});
}
});
您可以使用on()
和off()
进行类似的
var inProgress = false;
$("#submit").on("click", function() {
data = some data
if(data !== "" && !inProgress ){
inProgress = true;
$.ajax({
url: '/my url/',
type: 'POST',
contentType: 'application/json',
dataType: 'json',
data :data,
processData: false,
success: function(data){
$('#submit').off(); // If is correct remove all event listeners
},
error: function(jqXHR, textStatus, errorThrown){
loginFailed();
}
})
}
});
var loginFailed = function(){
inProgress = false;
alert("Some Error");
// Display any Errors
};
关闭:http://api.jquery.com/off
在:http://api.jquery.com/on
我们创建一个变量来检查是否有登录正在进行中,如果没有,我们可以尝试再次登录,如果有,则单击时不会发生任何事情,如果登录成功,则事件侦听器将被删除。
相关文章:
- Javascript返回值只在循环中返回一次
- Jquery FadeIn FadeOut 只工作一次
- Javascript html每点击一次就会更改url
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- Rails操作只调用一次,但我在ajax中每秒钟都调用一次
- jQuery滚动功能只工作一次
- 刷新导致我的帖子“;张贴“;再一次
- 引导程序崩溃一次只能看到一个
- mysql查询一次在单个查询中的多个表中插入记录
- Ajax发布到PHP脚本,每5秒查询一次MySQL数据库的性能/问题
- 简单的jQuery将单词的颜色一次查询为蓝色
- 如何每 5 分钟重复一次 mysql 查询
- j查询文档加载时,每 3 秒检查一次并重新加载
- jquery.one()触发了一次查询,但是如果.one(()内部出现问题,我怎么能触发一次查询呢
- <a>使用javascript:void(0)href一次使用后不调用ajax查询
- 缓存——一次用查询字符串更新多个js文件
- 如何在PHP和JavaScript中保持每分钟运行一次查询来检查数据库
- Fire脚本一次与JQuery窗口大小调整和媒体查询
- 每 5 秒调用一次 MySQL 查询
- 将MongoDB查询中的数据存储到对象中,每20分钟更新一次