简体   繁体   English

输入前执行onclick

[英]Execute onclick before input

This is difficult to approach because I'm running "Custom javascript for websites 2" Chrome extension. 这很难解决,因为我正在运行“网站2的自定义JavaScript” Chrome扩展程序。 So I can't modify the code , only I can add. 因此, 我无法修改代码 ,只能添加。

The webpage have a form which it's loaded doing an ajax request. 该网页具有一个通过ajax请求加载的表单。 Then, there is a button without form validation. 然后,有一个没有表单验证的按钮。 I want to add form validation . 我想添加表单验证

The default form submits the form with a onclick HTML attribute inside an input. 默认表单使用输入中带有onclick HTML属性的形式提交表单。

1- First failed approach: 1-第一种失败的方法:

document.getElementById('submit-btn').onclick = myValidation;

Result: it works the first time I press the button. 结果:它在我第一次按下按钮时有效。 But the second time, submits as default. 但是第二次,默认提交。

2- Second failed approach: 2-秒失败方法:

document.getElementById('submit-btn').addEventListener('click', myValidation, true);

Result: first do the default submit (onclick attribute) and then, do myValidation function. 结果:首先执行默认的提交(onclick属性),然后执行myValidation函数。 I thought what putting addEventListener true argument, I will fire first. 我以为将addEventListener设置为true参数是什么,我将首先触发。 But not. 但不是。

Is there a way for do that? 有办法吗?

in first case you should path reference to function NOT the function execute result 在第一种情况下,您应该路径引用函数而不是函数执行结果

INCORRECT: 不正确:

document.getElementById('submit-btn').onclick = myValidation();

CORRECT: 正确:

document.getElementById('submit-btn').onclick = myValidation;

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

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