无法阻止提交ajax加载的表单(Cannot prevent an ajax loaded form from submiting)

编程入门 行业动态 更新时间:2024-10-26 07:26:02
无法阻止提交ajax加载的表单(Cannot prevent an ajax loaded form from submiting)

我有一个网页,用户可以通过点击链接在页面上加载表单。 我使用ajax进行加载过程。 所有这些都发生在一个单独的主文件中。

表单加载代码

$(document).ready(function (){ $(".evaluation").click(function(event){ event.preventDefault(); var division = $("select#tmimata").prop("value"); var action = $(".lesson select").prop("value"); var table = $(".main-content table"); if (table.length > 0 ){ table.fadeOut(); } $(".main-content").load('/evaluation/employee/'+employee_id+"/?division="+tmima+"&action="+action, function(){ alert("successfully retrieved evaluation form"); }).show(); }); });

因此,它会检查主内容中是否存在先前加载的表格,然后将其删除并将该表格与类“main-content”加载到同一个div中。 在点击功能之外,我“捕捉”表单的提交事件

$(".evaluation_form").submit(function (event){ console.log('inside form submit'); var form = $(this); var tmima_id = $('select#divisions').prop('value'); var action_id = (".action select").prop('value'); data = form.serialize(); //ajax call to post the data event.preventDefault(); });

(我已经把preventDefault()放在提交的顶部,在底部,因为它现在仍然是一样的)

提交代码在点击功能之后,但在document.ready中。 但表单提交后无法使用。 我打赌它必须这样做,当页面加载(document.ready)时,它看不到表单(它不是在用户点击链接后加载的),因此它不会在形式正确吗? 我应该将提交函数放在“加载”回调函数中吗?

示例html代码

<!---somewhere in the html file --> <a href="#" class="evaluation">Evaluate employee</a> <div class="main-content"></div>

和形式

<form class="evaluaton_form" action="" method="post"> <!--form fields here --> <input type="submit" name="submit" value="Save evaluation" > </form>

I have a web page where a user can load a form on the page by hitting a link on it. I use ajax for the loading procedure. All this is happening in a seperate main file.

The form load code

$(document).ready(function (){ $(".evaluation").click(function(event){ event.preventDefault(); var division = $("select#tmimata").prop("value"); var action = $(".lesson select").prop("value"); var table = $(".main-content table"); if (table.length > 0 ){ table.fadeOut(); } $(".main-content").load('/evaluation/employee/'+employee_id+"/?division="+tmima+"&action="+action, function(){ alert("successfully retrieved evaluation form"); }).show(); }); });

So it checkes if there is a previous loaded table in the main-content and then it removes it and loads the form in the same div with class "main-content". After this outside the click function I "catch" the submit event for the form

$(".evaluation_form").submit(function (event){ console.log('inside form submit'); var form = $(this); var tmima_id = $('select#divisions').prop('value'); var action_id = (".action select").prop('value'); data = form.serialize(); //ajax call to post the data event.preventDefault(); });

(I have placed preventDefault() both at the top of submit and at the bottom as it is right now still the same)

The submit code is after the click function but inside the document.ready. But it won't work the form is submitted. I am betting it has to do that when the page loads(document.ready) it doesn't see the form (its not there it is loaded after a user clicks on a link) so it doesn't chatch the submit event on the form correct? Should I place the submit function inside the "load" callback function?

sample html code

<!---somewhere in the html file --> <a href="#" class="evaluation">Evaluate employee</a> <div class="main-content"></div>

and the form

<form class="evaluaton_form" action="" method="post"> <!--form fields here --> <input type="submit" name="submit" value="Save evaluation" > </form>

最满意答案

尝试这个:

$(document).on('submit', '.evaluation_form', function(event) { console.log('inside form submit'); var form = $(this); var tmima_id = $('select#divisions').prop('value'); var action_id = (".action select").prop('value'); data = form.serialize(); //ajax call to post the data event.preventDefault(); });

try this:

$(document).on('submit', '.evaluation_form', function(event) { console.log('inside form submit'); var form = $(this); var tmima_id = $('select#divisions').prop('value'); var action_id = (".action select").prop('value'); data = form.serialize(); //ajax call to post the data event.preventDefault(); });

更多推荐

本文发布于:2023-07-31 22:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1348992.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   加载   ajax   prevent   submiting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!