在具有动态值的函数上使用的变量不允许我通过 ajax 发送表单
Variable used on function with a dynamic value doesn't allow me to send form through ajax
我在一个页面上有很多答案表格,所有表格都有不同的类。
所有表单都有 2 个按钮来发送父表单。
如果单击#avote_down
或#avote_up
,我发送表单,然后单击按钮的父表单类并将类保存在var clase
中,然后添加. 在类名之前(我知道点很奇怪,但如果我不这样做,这不起作用),之后我将之前编辑的类保存在名为 answervotes
的 var 上,以便我们可以使用它。
//declare variables outside the functions to use them
//inside others
var answerdata;
var answervotes;
var clase;
var clasedot;
$('#avote_down, #avote_up').on("click",function(e) {
e.preventDefault();
clase = $(this).parents("form:first").attr('class');
clasedot = '.'+clase;
answervotes = $(clasedot);
answerdata = answervotes.serializeArray();
answerdata.push({name: encodeURI($(this).attr('name')), value: encodeURI($(this).attr('value'))});
answervotes.submit();
answerdata.pop();
});
如果一切顺利,我可以在下面使用 ajax 函数发送表单,如您所见,ajax 函数正在使用之前声明的 var。
answervotes.bind('submit',function () {
$.ajax({
url: answervotes.attr('action'),
type: answervotes.attr('method'),
cache: false,
dataType: 'json',
data: answerdata,
success: function(data) {
if(data.message == "plus")
{
$("#avote_up").attr('class','options options-hover');
$("#avote_down").attr('class','options');
$("#atotal-votes").html(data.votes);
console.log(data.votes);
}
if(data.message == "sub")
{
$("#avote_down").attr('class','options options-hover');
$("#avote_up").attr('class','options');
$("#atotal-votes").html(data.votes);
console.log(data.votes);
}
if(data.name == "register")
{
$('.theme-actions').append('<div class="should-login"><span>' + data.message + '</span><div id="close"></div></div>');
setTimeout(function() {
$('.should-login').fadeOut(300);
},4000);
setTimeout(function() {
$('.should-login').remove();
},4300);
}
if(data.name == "limited_votes")
{
$('.theme-actions').append('<div class="should-login"> <span>' + data.message + '</span><div id="close"></div></div>');
setTimeout(function() {
$('.should-login').fadeOut(300);
},4000);
setTimeout(function() {
$('.should-login').remove();
},4300);
}
if(data.name == "repeated_vote")
{
$('.theme-actions').append('<div class="should-login"><span>' + data.message + '</span><div id="close"></div></div>');
setTimeout(function() {
$('.should-login').fadeOut(300);
},4000);
setTimeout(function() {
$('.should-login').remove();
},4300);
}
},
error: function(xhr, textStatus, thrownError) {
console.log(data.message);
alert("error");
}
});
return false;
});
问题:当我尝试像这样发送表单时,它只是将我发送到表单操作页面,就像没有使用e.preventDefault()方法来阻止正常操作一样,但实际上它就在那里你看到了它。
重要事实:当我使用直接名称将值分配给 click 函数外部的 answervotes
var 时var answervotes = $(".parent-form1");
它可以完美运行,但如果我直接在点击函数中分配名称,它也不起作用(我需要这是动态的,具体取决于父表单)。
捕获的类型错误:无法读取未定义的属性"绑定",可能是因为在单击按钮之前不会获得answervotes
,但我想这将通过 var 问题来解决。
这是一个JSFIDDLE:https://jsfiddle.net/EnmanuelDuran/cyvpkvkk/22/
我认为您的 answervote 选择器不匹配,请使用控制台或警报以确保您得到了正确的选择.log:
$(document).ready(function() {
var answervotes = $(".parent-form1");
$('#avote_down, #avote_up').on("click",function(e) {
e.preventDefault();
answervotes.submit();
});
answervotes.on("submit", function(e) {
e.preventDefault();
//do ajax
});
});
这是一个JSFIDDLE:https://jsfiddle.net/cyvpkvkk/
已解决。
我将提交功能嵌套到单击函数中,并在执行提交功能后执行表单提交和以下操作:
$('#avote_down, #avote_up').on("click",function(e) {
e.preventDefault();
clase = $(this).parents("form:first").attr('class');
clasedot = '.'+clase;
answervotes = $(clasedot);
answerdata = answervotes.serializeArray();
answervotes.bind('submit',function () {
$.ajax({
url: answervotes.attr('action'),
type: answervotes.attr('method'),
cache: false,
dataType: 'json',
data: answerdata,
success: function(data) {
if(data.message == "plus")
{
$("#avote_up").attr('class','options options-hover');
$("#avote_down").attr('class','options');
$("#atotal-votes").html(data.votes);
console.log(data.votes);
}
if(data.message == "sub")
{
$("#avote_down").attr('class','options options-hover');
$("#avote_up").attr('class','options');
$("#atotal-votes").html(data.votes);
console.log(data.votes);
}
if(data.name == "register")
{
$('.theme-actions').append('<div class="should-login"><span>' + data.message + '</span><div id="close"></div></div>');
setTimeout(function() {
$('.should-login').fadeOut(300);
},4000);
setTimeout(function() {
$('.should-login').remove();
},4300);
}
if(data.name == "limited_votes")
{
$('.theme-actions').append('<div class="should-login"> <span>' + data.message + '</span><div id="close"></div></div>');
setTimeout(function() {
$('.should-login').fadeOut(300);
},4000);
setTimeout(function() {
$('.should-login').remove();
},4300);
}
if(data.name == "repeated_vote")
{
$('.theme-actions').append('<div class="should-login"><span>' + data.message + '</span><div id="close"></div></div>');
setTimeout(function() {
$('.should-login').fadeOut(300);
},4000);
setTimeout(function() {
$('.should-login').remove();
},4300);
}
},
error: function(xhr, textStatus, thrownError) {
console.log(data.message);
alert("error");
}
});
return false;
});
answerdata.push({name: encodeURI($(this).attr('name')), value: encodeURI($(this).attr('value'))});
answervotes.submit();
answerdata.pop();
});
它解决了基本问题,然后我使用了唯一的表单类并使用表单父类调用其中的元素,这样我就可以只调用该元素,即使它们是像那个一样调用的更多 id,它也不会显示问题,因为每个表单都有一个唯一的类来调用其上的嵌套元素。
记录:变量位于函数外部,在 document.ready 上
- XMLHttpRequest无法加载url Origin访问控制允许Origin不允许null
- 访问控制允许方法不允许删除
- http://0.0.0.0:4567访问控制允许来源不允许.使用Backbone&Firebase
- D3 Json请求获取XmlHttpRequest错误:访问控制允许原点不允许原点为null
- Jquery以使用WCF.错误:XMLHttpRequest无法加载Origin.访问控制允许Origin不允许null
- 检查数组是否包含允许或不允许的字符
- 简单<脚本>标记 - 访问控制允许原点不允许原点空
- XMLHttpRequest 无法加载 http://localhost:8089/jquery.访问控制允许原点不允许
- 访问控制允许原点不允许源 http://127.0.0.1
- 访问控制允许源不允许 AJAX 源空
- 交叉请求时出错:“访问控制允许源不允许源”
- 访问控制允许原点不允许原点
- 访问控制允许源不允许 http://example.com
- XMLHttpRequest 无法加载 url.预检响应中的访问控制允许方法不允许方法 PUT
- XMLHttpRequest 无法加载 URL.访问控制允许源不允许的源
- "访问控制允许原点不允许原点为null"使用jQuery调用web服务时出错
- Meteor Restivus:飞行前响应中的访问控制允许方法不允许方法PUT
- XMLHttpRequest无法加载“;此URL“;起源http://localhost:3000访问控制允许来源不允许
- 为什么我看到一个“;“访问控制允许起源”不允许起源;此处出现错误
- 推特:仅应用程序身份验证错误访问控制允许原点不允许原点为null