取消表单提交 [英] Cancel form submit

查看:113
本文介绍了取消表单提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整个上午都在尝试取消表单提交到服务器。


我有一个JavaScript对象,用于确定是否应该将页面提交给服务器,具体取决于用户是否点击是。或不。


此对象附加到页面上的各种HTML元素(按钮,选择等),并捕获它们引发的任何导致页面提交的事件。

如果用户没有响应,Object 应该取消提交给服务器的页面。


这对Button Click事件很有效到目前为止。


当一个JavaScript函数在Select Object的选定索引发生变化时将Form提交给服务器时,我的问题就出现了。


我的对象被实现为提示用户并让他们在Select的选定索引发生变化但是页面继续提交时取消该事件,因为附加到Select的其他JavaScript函数提交了表单。显然,当这个JavaScript函数将页面提交给服务器时,OnSubmit被完全绕过。从来没有调用过我的函数。


当Select导致对服务器的异步调用(Ajax)时,我能够中止/取消发生的提交。


当Select导致整页提交到服务器时,没有停止它。


以下代码位于JavaScript Object的初始化实现中。它创建了一个代表。表单提交到服务器时应该调用的函数。 (createDelegate方法基本上使用apply方法在处理事件的函数中使Object可用...参见这里如果您有兴趣可以获得更多信息)

I''ve been trying all morning to cancel a form submit to the server.

I have a JavaScript Object that determines whether or not the page should be submitted to the server depending on whether the user clicks "yes" or "no".

This Object is attached to various HTML elements on the page (buttons, selects etc) and traps any events that they raise that cause the page to submit.

If the user has not responded, the Object should cancel the page submit to the server.

This is working well for Button Click events so far.

My problem happens when a JavaScript function submits the Form to the server when a Select Object''s selected index changes.

My Object is implemented to prompt the user and let them cancel the event when the Select''s selected index changes but the page proceeds to submit because the other JavaScript function attached to the Select submits the form. Apparently when the page is submitted to the server by this JavaScript function, the OnSubmit is completely "bypassed" and my function is never called.

When the Select causes an asynchronous call to the server (Ajax) I am able to abort/cancel the submit from happening.

When the Select causes a full page submit to the server there''s not stopping it.


The following code is in the JavaScript Object''s initialize implementation. It creates a "delegate" to a function that should be called when the form submits to the server. (The createDelegate method basically uses the apply method to make the Object available in the function that handles the event...see here for more information if you''re interested)

展开 | 选择 | Wrap | 行号

推荐答案

因为你的onsubmit函数通常只返回一个布尔值

你要验证的任何地方首先运行该函数

if(onsubmitfunction == true){form.submit} else {// go crazy}


javascript将不会在表单提交时进行回调你使用form.submit
since your onsubmit function normally just returns a boolean value
everywhere you want to validate you run through that function first
if (onsubmitfunction==true) { form.submit } else { //go crazy }


javascript will not do a callback on a form submit when you use form.submit


抱歉,但我不明白。


当我的对象在浏览器中加载时,初始化函数被调用。


此时它调用一个循环遍历元素id数组的函数,它需要在提交之前进行确认,并且它会动态地为任何导致这些事件的事件应用事件处理程序。提交表单的元素。

处理事件的方法显示带有消息的DOM元素和2个(或更多)按钮,允许用户确认或拒绝操作。 />

一旦用户确认或拒绝了该操作,对象将关闭提示用户的DOM并且不执行任何其他操作,否则它将重新发出为执行提交表单而执行的原始事件正如通常所拥有的那样。


在将事件处理程序动态应用于与Object关联的每个控件的方法中,我基本上是运行。 验证功能第一....


嗯我想我之前从未说过......但是我无法阻止选择元素在此时提交,这是为什么我最终试图通过实现一个处理onsubmit事件的函数来阻止表单提交[/ edit]
Sorry but I don''t understand.

When my Object is loaded in the browser the "initialize" function is called.

At that point it calls a function that loops through an array of element ids which require confirmation before submitting and it dynamically applies event handlers for any events that would cause the these elements to submit the form.

This method that handles the events displays a DOM element with a message and 2 (or more) buttons allowing the user to confirm or deny the action.

Once the user has confirmed or denied the action, the Object will either close the DOM prompting the user and do nothing else or it will reissue the original event that was executed to submit the form the server as it normally would have.

In the method that dynamically applies event handlers to every control associated with the Object I am essentially "run through" the "validation" function first....

[edit]Hmm I guess I never stated this before.... but I was unable to stop the select element from submitting at this point and that is why I ended up resorting to trying to stop the form from submitting by implementing a function that handles the onsubmit event[/edit]


确实你实际上有点意义。


我想我应该一直试图解决原来的问题而不是解决它只是为了再次遇到同样的问题。


原始问题必须停止select的选定索引更改事件,以防止与select相关的JavaScript被执行。


以下是循环遍历所有与Object关联的元素的ID,并动态地将事件处理程序添加到这些元素的事件中,这些元素可能导致页面使用
Ok you are making a bit of sense actually.

I guess I should have been trying to solve the original problem instead of working around it only to run into the same problem again.

The original problem had to do stopping the select''s selected index changed event in order to prevent the JavaScript associated with the select from being executed.

The following is the function that loops through all of the IDs for the elements associated with the Object and dynamically adds an event handler to the events for these elements that may cause the page to submit using the

提交

这篇关于取消表单提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆