【问题标题】:Ajax request triggers submit.... why?Ajax 请求触发提交....为什么?
【发布时间】:2017-02-22 10:16:04
【问题描述】:

所以我有一些东西可能看起来有点小,但就是这样。

这一切都是使用 jQuery 完成的

我有一个函数“cont_refresh”,用于刷新页面内容等,其中包含一个 AJAX 请求,用于将变量传递给外部文档/脚本进行处理。

除此之外,我还有一个匿名函数,应该在表单提交时触发......这意味着完全不相关,不应该在按下此按钮时发生。

然而,当我运行“cont_refresh”时,提交功能被触发,它似乎触发提交功能的点是我开始发出我的 ajax 请求。

最终发生的事情是我发出 ajax 请求,一个只在服务器上运行一个函数,第二个是提交我的表单......我不希望后者发生。

有两个按钮分别处理这两个事情,一个是提交按钮,另一个是与此问题相关的不是提交按钮。

为什么?以及如何防止这种情况发生?

你的脑海中会盘旋着一个问题,为什么这个小丑有两个功能基本上做同样的事情,但这是另一个主题,我会优雅地请求你提出一个新的帖子;)。

/*
  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>

【问题讨论】:

  • 使用 e.preventDefault();其中 e 是事件处理程序

标签: php jquery ajax forms submit


【解决方案1】:

好的,你可以试试这个...

给你的表单一个 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 .
});

看看有没有帮助。

【讨论】:

  • 这个函数处理的表单很多,所以我不想指定表单名称。此功能完美运行,问题是,它不应该在此事件中触发。我可以看到我的问题并不完全清楚,但 cont_refresh 函数是我想要触发的。我确实触发了,但是当它到达 ajax 调用时,触发了全局提交函数,不明白为什么。
  • 好吧,如果您要尝试一下,那么问题是否会消失,这与@Nitin 在他的回答中所建议的有关。至少这可能有助于了解正在发生的事情。
  • 所以这告诉我的是,即使按钮不是提交按钮,它确实是在提交我的表单。
  • 好吧,如果你改变你的按钮,在它的声明中包含 type="button" 怎么办。这应该会阻止它默认为 type="submit"
  • 我已经完成并提交了它作为答案。感谢您的帮助。
【解决方案2】:

您已经在全局范围内添加了 form.submit 函数,这显然会在页面刷新后(即页面加载时)被调用。

在按钮点击事件或其他事件上添加您的 $('form').submit() 函数。

您的 ajax 没有提交表单,由于全局函数 $('form').submit()

$('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 .
});

【讨论】:

  • 不确定是不是这样。
  • 不确定是不是这样....不完全是这样吗?该按钮不是提交按钮,因此不应触发全局提交操作(据我所知)。我正在调用的函数正在被触发,但是当我的函数到达 AJAX 请求时它也会触发提交函数。附带说明一下,如何删除我之前的不完整评论?
【解决方案3】:

答案与按钮有关。

我没有意识到通过使用 HTML5 按钮,我需要指定一种按钮类型来防止默认的提交操作。

<button>This will Submit</button>
<button type="button">Will not Submit</button>

感谢@TimBrownlaw 和@Nitin 指导我找到解决方案。

【讨论】:

    猜你喜欢
    • 2018-08-29
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多