Php 两个ajax同时发布在一个链接上,单击jquery
我正在使用一个链接,它有类名Php 两个ajax同时发布在一个链接上,单击jquery,php,jquery,Php,Jquery,我正在使用一个链接,它有类名next和idend 在clcik上,类名和id我都在使用jQueryPost 我遇到的问题是,有时ajax请求会在一次单击中触发多次。单击一次,我会从一个url获取数据,同时通过另一个url将这些数据保存到db中。因此,有时插入db时会出现一些问题。有时会输入空值,有时会有多行进入db。那么,我该如何处理编写这两个函数,使它们都能完美工作 $('.next').live('click', function (e) { e.preventDefault();
next
和idend
在clcik上,类名和id我都在使用jQueryPost
我遇到的问题是,有时ajax请求会在一次单击中触发多次。单击一次,我会从一个url获取数据,同时通过另一个url将这些数据保存到db中。因此,有时插入db时会出现一些问题。有时会输入空值,有时会有多行进入db。那么,我该如何处理编写这两个函数,使它们都能完美工作
$('.next').live('click', function (e) {
e.preventDefault();
var result = [];
var answer = [];
var le = '';
$('.answertext').each(function (index, element) {
result.push($(this).val());
});
$('.answer').each(function (index, element) {
answer.push($(this).val());
});
le = $('#level').val();
mle = $('#mainlevel').val();
$.ajax({
url: 'matchanswers.php',
type: 'POST',
data: {
result: result,
answer: answer,
level: le,
mle: mle
},
async: true,
beforeSend: function () {
// show indicator
},
complete: function () {
// hide indicator
},
success: function (data) {
$('.quizform').html(data);
}
});
});
$('#end').live('click', function (e) {
e.preventDefault();
var sublev = $('#level').val();
var score = $('#count').val();
if (sublev < 11) {
$.ajax({
url: 'submitanswers.php',
type: 'POST',
data: {
sublev: sublev,
score: score
},
async: true,
beforeSend: function () {
// show indicator
},
complete: function () {
// hide indicator
},
success: function (data2) {}
});
} else {
$.ajax({
url: 'getanswers.php',
type: 'POST',
data: {
sublev: sublev,
score: score
},
async: true,
beforeSend: function () {
// show indicator
},
complete: function () {
// hide indicator
},
success: function (data3) {
if (data3) {
$('.quizform').html("");
$('form :input').attr('disabled', 'disabled');
$('#logout').removeAttr("disabled");
var obj = $.parseJSON(data3);
$('#sum').html("Your Total Score for level - " + obj[0] + " is " + obj[1] + " in " + obj[2] + "secs");
}
}
});
}
});
$('.next').live('click',函数(e){
e、 预防默认值();
var结果=[];
var-answer=[];
变量le='';
$('.answertext')。每个(函数(索引,元素){
result.push($(this.val());
});
$('.answer')。每个(函数(索引,元素){
answer.push($(this.val());
});
le=$('#level').val();
mle=$('#mainlevel').val();
$.ajax({
url:'matchanswers.php',
键入:“POST”,
数据:{
结果:结果,,
答:答,,
级别:le,
mle:mle
},
async:true,
beforeSend:函数(){
//显示指示器
},
完成:函数(){
//隐藏指示器
},
成功:功能(数据){
$('.quizform').html(数据);
}
});
});
$('#end').live('click',函数(e){
e、 预防默认值();
var subsev=$('#level').val();
变量得分=$(“#计数”).val();
如果(次级V<11){
$.ajax({
url:'submitanswers.php',
键入:“POST”,
数据:{
转租:转租,,
分数:分数
},
async:true,
beforeSend:函数(){
//显示指示器
},
完成:函数(){
//隐藏指示器
},
成功:函数(data2){}
});
}否则{
$.ajax({
url:'getanswers.php',
键入:“POST”,
数据:{
转租:转租,,
分数:分数
},
async:true,
beforeSend:函数(){
//显示指示器
},
完成:函数(){
//隐藏指示器
},
成功:功能(数据3){
如果(数据3){
$('.quizform').html(“”);
$('form:input').attr('disabled','disabled');
$(“#注销”).removeAttr(“禁用”);
var obj=$.parseJSON(数据3);
$('#sum').html(“您的级别-“+obj[0]+”的总分是“+obj[1]+”中的“+obj[2]+”秒”);
}
}
});
}
});
只需检查事件触发器,如:
$('.next').live('click', function (e) {
if(e.handled !== true){ // This will prevent event triggering more then once
e.handled = true;
//Your code
}
});
$('#end').live('click', function (e) {
if(e.handled !== true){ // This will prevent event triggering more then once
e.handled = true;
//Your code
}
});
通过这样做,您将停止多事件触发器,这是一个非常常见的问题,应该可以解决您的问题
编辑:
您的完整代码将是:
$('.next').live('click', function (e) {
if (e.handled !== true) { // This will prevent event triggering more then once
e.handled = true;
//Your code
e.preventDefault();
var result = [];
var answer = [];
var le = '';
$('.answertext').each(function (index, element) {
result.push($(this).val());
});
$('.answer').each(function (index, element) {
answer.push($(this).val());
});
le = $('#level').val();
mle = $('#mainlevel').val();
$.ajax({
url: 'matchanswers.php',
type: 'POST',
data: {
result: result,
answer: answer,
level: le,
mle: mle
},
async: true,
beforeSend: function () {
// show indicator
},
complete: function () {
// hide indicator
},
success: function (data) {
$('.quizform').html(data);
}
});
}
});
$('#end').live('click', function (e) {
if (e.handled !== true) { // This will prevent event triggering more then once
e.handled = true;
//Your code
e.preventDefault();
var sublev = $('#level').val();
var score = $('#count').val();
if (sublev < 11) {
$.ajax({
url: 'submitanswers.php',
type: 'POST',
data: {
sublev: sublev,
score: score
},
async: true,
beforeSend: function () {
// show indicator
},
complete: function () {
// hide indicator
},
success: function (data2) {}
});
} else {
$.ajax({
url: 'getanswers.php',
type: 'POST',
data: {
sublev: sublev,
score: score
},
async: true,
beforeSend: function () {
// show indicator
},
complete: function () {
// hide indicator
},
success: function (data3) {
if (data3) {
$('.quizform').html("");
$('form :input').attr('disabled', 'disabled');
$('#logout').removeAttr("disabled");
var obj = $.parseJSON(data3);
$('#sum').html("Your Total Score for level - " + obj[0] + " is " + obj[1] + " in " + obj[2] + "secs");
}
}
});
}
}
});
$('.next').live('click',函数(e){
如果(e.handled!==true){//这将防止事件触发一次以上
e、 已处理=正确;
//你的代码
e、 预防默认值();
var结果=[];
var-answer=[];
变量le='';
$('.answertext')。每个(函数(索引,元素){
result.push($(this.val());
});
$('.answer')。每个(函数(索引,元素){
answer.push($(this.val());
});
le=$('#level').val();
mle=$('#mainlevel').val();
$.ajax({
url:'matchanswers.php',
键入:“POST”,
数据:{
结果:结果,,
答:答,,
级别:le,
mle:mle
},
async:true,
beforeSend:函数(){
//显示指示器
},
完成:函数(){
//隐藏指示器
},
成功:功能(数据){
$('.quizform').html(数据);
}
});
}
});
$('#end').live('click',函数(e){
如果(e.handled!==true){//这将防止事件触发一次以上
e、 已处理=正确;
//你的代码
e、 预防默认值();
var subsev=$('#level').val();
变量得分=$(“#计数”).val();
如果(次级V<11){
$.ajax({
url:'submitanswers.php',
键入:“POST”,
数据:{
转租:转租,,
分数:分数
},
async:true,
beforeSend:函数(){
//显示指示器
},
完成:函数(){
//隐藏指示器
},
成功:函数(data2){}
});
}否则{
$.ajax({
url:'getanswers.php',
键入:“POST”,
数据:{
转租:转租,,
分数:分数
},
async:true,
beforeSend:函数(){
//显示指示器
},
完成:函数(){
//隐藏指示器
},
成功:功能(数据3){
如果(数据3){
$('.quizform').html(“”);
$('form:input').attr('disabled','disabled');
$(“#注销”).removeAttr(“禁用”);
var obj=$.parseJSON(数据3);
$('#sum').html(“您的级别-“+obj[0]+”的总分是“+obj[1]+”中的“+obj[2]+”秒”);
}
}
});
}
}
});
你是
$('.next').live('click', function(e)
$('#end').live('click', function(e)
$('.next').live('click', function(e) { ...
success: function(data) { $.ajax({
url: 'submitanswers.php', }