简体   繁体   English

如何防止表格提交冒泡

[英]How to Prevent Bubbling For Form Submit

I have form that calls the function GenerateWords when it is submitted and returns false. 我有一个表单,在提交时调用函数GenerateWords并返回false。

<form id="3Form" onsubmit="GenerateWords(this); return false;">

This is causing problems with Google Tag Manager implementation as it does not bubble up to the form submit listener. 这会导致Google跟踪代码管理器实施出现问题,因为它不会冒泡到表单提交侦听器。

I understand event.preventDefault(); 我理解event.preventDefault(); needs to be used and return false removed but don't know how to implement this. 需要使用并返回false删除但不知道如何实现它。 The current javascript I have is: 我当前的javascript是:

function GenerateWords(F) {
var strWords = F.words.value;
if ... condition is false
    return false;
}
if ... condition is false
    return false;
}
vars declared
for (var i = 0; i < ctLines; i++) {
    var strLine = arrLines[i];
    strLine = Trim(strLine.replace(/[\r]/g,""));
    if successful condition
    }
}
F.result.value = oResult.join("");
F.result.focus();
}

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks. 谢谢。

Try this in javascript: 在javascript中试试这个:

 function GenerateWords(F,ev) {  // event object 
    ...
    if(someCondition)  // condition when the form should not be submitted.
    ev.preventDefault();
    }

and you may remove return false; 你可以删除return false; from the form tag and pass the event reference 从表单标记并传递事件引用

<form id="3Form" onsubmit="GenerateWords(this,event);">

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

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