简体   繁体   English

如何在JavaScript中添加Onsubmit()功能

[英]How to add Onsubmit () functionality in javascript

My Webpage using 1000s of the html page and its header and footers are maintained in single Javascript file. 我的使用html页数以千计的网页及其页眉和页脚都保存在单个Javascript文件中。

An issue is, if any new link is opened from my web page the newly opened page is auto-refreshing. 问题是,如果从我的网页中打开了任何新链接,则新打开的页面会自动刷新。

Adding onSubmit="return false" in my HTML files will solve the issue. 在我的HTML文件中添加onSubmit="return false"将解决此问题。 But It's not possible to add the same in 1000s of HTML files. 但是,不可能在数千个HTML文件中添加相同的内容。

please let me know is there any way by which I can control this auto refreshing issue and add this functionality globally once in my Javascript file? 请让我知道,有什么方法可以控制此自动刷新问题并在Javascript文件中一次全局添加此功能?

You can first of all find all form in page. 您可以首先在页面中找到所有表格。 Using following code & add onsubmit event dynamically using jQuery. 使用以下代码并使用jQuery动态添加onsubmit事件。

You can put this code in your common javascript file. 您可以将此代码放入普通的javascript文件中。

 $(document).ready(function(){ $("form").each(function() { $(this).attr("onsubmit", "return false"); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="a.php" method="post" id="form1"> <input type="text" name="name1"> <input type="submit" name="submit1"> </form> <form action="b.php" method="post" id="form2"> <input type="text" name="name2"> <input type="submit" name="submit2"> </form> 

Try this: 尝试这个:

window.onload = function() {
    var form = document.getElementById('hosted_payment_form');
    form.onsubmit = function() {
        _gaq.push(['_linkByPost', this]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM