gpt4 book ai didi

Javascript onSubmit 不返回 false

转载 作者:行者123 更新时间:2023-12-03 04:59:40 25 4
gpt4 key购买 nike

我的 onsubmit 代码有问题。我正在使用第三方公司的计算器,并尝试在提交表单时触发一些 JavaScript。警报将触发,但如果我将返回设置为 false,表单仍会提交。

这是我在自己的环境中使用的代码行:document.getElementsByTagName("Form")[3].onsubmit = function () {alert('测试');返回假; };

我在这里建立了一个副本:(无论出于什么原因它不会加载到代码片段中,我将相同的代码复制到我的服务器并且它工作正常,所以这里是它的实际操作。Page)

function myFunction() {
document.getElementsByTagName("Form")[0].onsubmit = function () { alert('Test'); return false; };
}
<button onclick="myFunction()">Wait for script to load then press this button to add the onsubmit, then you can press calculate</button>
<script src="https://calculators.symmetry.com/widget/js/salary.js?key=RHA0Slc2ZjEydjNkR0Y2eTEvOUxpZz09"></script>

我还没有弄清楚如何对脚本进行加载检测,因此您必须等待脚本加载,然后按按钮插入 JavaScript。

感谢您的帮助。

最佳答案

好吧..在您的解决方案中,您将在每次单击按钮时添加一个新的事件处理程序到表单

而不是在页面加载时声明事件处理程序然后触发它

应该是......

<script>
function onMyPageLoaded()
{
document.getElementsByTagName("Form")[0].onsubmit = function (e)
{
e.preventDefault();

// place your code here

return false; // cancel form submit
};
}

function doSomething() // use this ONLY if you intend to by-pass the native submit mechanism
{
document.getElementsByTagName("Form")[0].submit(); // trigger the event here
}
</script>

<body onload="onMyPageLoaded()">
<form>
<button type="submit">send</button>
</form>
</body>

关于Javascript onSubmit 不返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42284018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com