Ajax 请求触发提交....为什么?
Ajax request triggers submit.... why?
所以我有一些看起来有点琐碎的东西,但是就这样了。
这一切都是使用jQuery
完成的
我有一个函数 "cont_refresh" 用于刷新页面内容等,其中包含 AJAX 将变量传递给外部 document/script 进行处理的请求。
除此之外,我还有一个匿名函数,应该在表单提交时触发...这意味着完全不相关,不应在按下此按钮时发生。
然而,当我 运行 "cont_refresh" 提交功能被触发时,似乎触发提交功能的时间点是我开始制作 ajax要求。
最终发生的事情是我向 ajax 发出请求,第一个请求只是 运行 服务器上的一个功能,第二个是提交我的表单...我不不希望后者发生。
有两个按钮分别处理这两个事情,一个是提交按钮,另一个与此问题相关的不是提交按钮。
为什么?我该如何防止这种情况?
你的脑海中可能会出现这样的疑问,为什么这个小丑有两个基本上做同样事情的函数,但这是针对另一个主题的,我会优雅地请求你提出一个新的 post for ; ).
/*
This is what I want to run... it does but fails then.....(see below this function)
*/
function cont_refresh(form,params,url,e,modal){
$(".ng-loading-container").toggleClass("ng-loading-container-show");
if(modal === 'undefined'){
modal = false;
}
var request = $.ajax({
method: 'post',
url: url,
data: params,
success: function(result){
if(e !== '' && e !== 'undefined'){
$(e).html(result);
} else {
alert('Success');
}
},
error: function(){
alert('Page Error')
$(".ng-loading-container").toggleClass("ng-loading-container-show");
},
}).fail(function() {
alert( "Something went wrong!" );
$(".ng-loading-container").toggleClass("ng-loading-container-show");
})
// end of cont_refresh()
}
/*
.... This runs. Which is not my intention!
*/
$('form').submit(function (e){
e.preventDefault();
submit_form(e.target);
// there is an additional function that handles any submition from this point but have not included it as .
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Rightly or wrongly the intention here is to run a stored SQL procudure.<br>
<p>The reason the statements are stored in the db is so anyone (with access) can add and edit these statements or functions from my front end application.</p>
<form role="form" action="myscript.php" method="post">
<input type="hidden" name="stp_tsk_id" id="stp_tsk_id" class="form-control" value="34">
<div class="form-group col-md-12">
<label for="stp_function">Function (SQL statement)</label>
<textarea class="form-control" name="stp_function" id="stp_function" rows="3">SELECT * FROM SOME_TABLE</textarea>
</div>
<!-- this button should trigger only the function it is calling onclick -->
<button role="button"
class="btn btn-default btn-md"
onclick="cont_refresh('',{'id':34},'app/handlers/process_steps.php')">
Run this Step
</button>
<!-- There is a second "submit" button but not relevent for this query -->
<button role="submit" class="btn btn-default btn-md">Save</button>
</form>
好的,你可以试试这个...
给你的表格一个ID,比如
<form role="form" action="myscript.php" method="post" id="myForm">
但请随意使用 id 名称更有创意...
并将您的活动更改为
$('#myForm').submit(function (e){
e.preventDefault();
submit_form(e.target);
// there is an additional function that handles any submition from this point but have not included it as .
});
看看是否有帮助。
您已经在全局添加了 form.submit 函数,这显然会在页面刷新后调用,即在页面加载时调用。
在按钮点击事件或其他事件上添加 $('form').submit() 函数。
您的 ajax 没有提交表单,由于全局函数 $('form').submit()[=11,它正在提交=]
$('form').submit(function (e){ // instead of form use here button click event
e.preventDefault();
submit_form(e.target);
// there is an additional function that handles any submition from this point but have not included it as .
});
答案与按钮有关。
我没有意识到,通过使用 HTML5 按钮,我需要指定一种按钮来防止提交的默认操作。
<button>This will Submit</button>
<button type="button">Will not Submit</button>
感谢@TimBrownlaw 和@Nitin 指导我找到解决方案。
所以我有一些看起来有点琐碎的东西,但是就这样了。
这一切都是使用jQuery
完成的我有一个函数 "cont_refresh" 用于刷新页面内容等,其中包含 AJAX 将变量传递给外部 document/script 进行处理的请求。
除此之外,我还有一个匿名函数,应该在表单提交时触发...这意味着完全不相关,不应在按下此按钮时发生。
然而,当我 运行 "cont_refresh" 提交功能被触发时,似乎触发提交功能的时间点是我开始制作 ajax要求。
最终发生的事情是我向 ajax 发出请求,第一个请求只是 运行 服务器上的一个功能,第二个是提交我的表单...我不不希望后者发生。
有两个按钮分别处理这两个事情,一个是提交按钮,另一个与此问题相关的不是提交按钮。
为什么?我该如何防止这种情况?
你的脑海中可能会出现这样的疑问,为什么这个小丑有两个基本上做同样事情的函数,但这是针对另一个主题的,我会优雅地请求你提出一个新的 post for ; ).
/*
This is what I want to run... it does but fails then.....(see below this function)
*/
function cont_refresh(form,params,url,e,modal){
$(".ng-loading-container").toggleClass("ng-loading-container-show");
if(modal === 'undefined'){
modal = false;
}
var request = $.ajax({
method: 'post',
url: url,
data: params,
success: function(result){
if(e !== '' && e !== 'undefined'){
$(e).html(result);
} else {
alert('Success');
}
},
error: function(){
alert('Page Error')
$(".ng-loading-container").toggleClass("ng-loading-container-show");
},
}).fail(function() {
alert( "Something went wrong!" );
$(".ng-loading-container").toggleClass("ng-loading-container-show");
})
// end of cont_refresh()
}
/*
.... This runs. Which is not my intention!
*/
$('form').submit(function (e){
e.preventDefault();
submit_form(e.target);
// there is an additional function that handles any submition from this point but have not included it as .
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Rightly or wrongly the intention here is to run a stored SQL procudure.<br>
<p>The reason the statements are stored in the db is so anyone (with access) can add and edit these statements or functions from my front end application.</p>
<form role="form" action="myscript.php" method="post">
<input type="hidden" name="stp_tsk_id" id="stp_tsk_id" class="form-control" value="34">
<div class="form-group col-md-12">
<label for="stp_function">Function (SQL statement)</label>
<textarea class="form-control" name="stp_function" id="stp_function" rows="3">SELECT * FROM SOME_TABLE</textarea>
</div>
<!-- this button should trigger only the function it is calling onclick -->
<button role="button"
class="btn btn-default btn-md"
onclick="cont_refresh('',{'id':34},'app/handlers/process_steps.php')">
Run this Step
</button>
<!-- There is a second "submit" button but not relevent for this query -->
<button role="submit" class="btn btn-default btn-md">Save</button>
</form>
好的,你可以试试这个...
给你的表格一个ID,比如
<form role="form" action="myscript.php" method="post" id="myForm">
但请随意使用 id 名称更有创意...
并将您的活动更改为
$('#myForm').submit(function (e){
e.preventDefault();
submit_form(e.target);
// there is an additional function that handles any submition from this point but have not included it as .
});
看看是否有帮助。
您已经在全局添加了 form.submit 函数,这显然会在页面刷新后调用,即在页面加载时调用。
在按钮点击事件或其他事件上添加 $('form').submit() 函数。
您的 ajax 没有提交表单,由于全局函数 $('form').submit()[=11,它正在提交=]
$('form').submit(function (e){ // instead of form use here button click event
e.preventDefault();
submit_form(e.target);
// there is an additional function that handles any submition from this point but have not included it as .
});
答案与按钮有关。
我没有意识到,通过使用 HTML5 按钮,我需要指定一种按钮来防止提交的默认操作。
<button>This will Submit</button>
<button type="button">Will not Submit</button>
感谢@TimBrownlaw 和@Nitin 指导我找到解决方案。