执行表单提交功能 [英] Perform function on form submit

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

问题描述

希望这是一个非常简单的问题!我有jQuery中的代码来监听要提交的页面上的特定表单,并在它出现时执行警报。即:表单本身没有ID,因此我将该表单定位在特定DIV ID。

  $(#content form)。submit(function(){
alert(lorem ipsum dolor?);
});

不使用jquery就可以在javascript中执行此操作的语法是什么?例如,我有这个代码(下面),只是不确定如何侦听表单被提交来执行一个操作..

  var submitform = document.getElementById(content)。getElementsByTagName(form); 

谢谢!

解决方案<

  var要在跨浏览器中执行,不是破坏性的方式,它需要一些代码:

提交formform = document.getElementById(content)。getElementsByTagName(form)[0],
callback = function(){
alert(lorem ipsum dolor?);
};


if(window.addEventListener){
submitform.addEventListener(submit,callback,false); //最新的浏览器
} else if(window.attachEvent){
submitform.attachEvent(onsubmit,callback); // IE
} else {
submitform.onsubmit = callback; //销毁其他处理程序
}


Hopefully this is a pretty simple question! I've got the code working in jQuery to listen for a specific form on a page to be submitted and perform an alert when it is.. I.e.: The form itself doesn't have an ID so I am targeting the form within a specific DIV ID.

$("#content form").submit(function() {
    alert("lorem ipsum dolor?");
});

What would be the syntax for performing this in javascript alone without using jquery? For example, I have this code (below) and just am unsure of how to listen for the form to be submitted to perform an action..

var submitform = document.getElementById("content").getElementsByTagName("form");

Thanks!

解决方案

To do it in a cross browser, not destructive way, it takes a bit of code:

var submitform = document.getElementById("content").getElementsByTagName("form")[0],
    callback = function(){
       alert("lorem ipsum dolor?");
    };


if(window.addEventListener){
   submitform.addEventListener("submit", callback, false); // Most modern browsers
} else if (window.attachEvent){
   submitform.attachEvent("onsubmit", callback); // IE
} else {
   submitform.onsubmit = callback; // Destroys other handlers
}

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

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