简体   繁体   English

表单操作覆盖 onClick 函数

[英]Form action overriding onClick function

I have a form with buttons in it and when you click on the button it is supposed to do two things, 1. post the button value which displays an element on the screen and 2. trigger some JS so that a second element will appear on the screen.我有一个带有按钮的表单,当您单击按钮时,它应该做两件事,1. 发布在屏幕上显示元素的按钮值和 2. 触发一些 JS,以便第二个元素出现屏幕。 The element that is supposed to appear with the onClick event pops up for a split second but then disappears while the element that is supposed to appear as a result of the post stays the whole time.应该与 onClick 事件一起出现的元素会弹出一瞬间,然后消失,而应该作为帖子结果出现的元素会一直停留。

<form action="index.php" method="post">
    <button 
        onclick="showDowntown()" 
        name="departmentbutton"
        type="submit" 
        value="601"
    >
        601
    </button>
function showDowntown() {
    var x = document.getElementById("Downtown");

    if (x.style.display === "") {
        x.style.display = "block";
    } else {
        x.style.display = "";
    }
}

See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formaction请参阅: https ://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formaction

The URL that processes the information submitted by the button.处理按钮提交的信息的 URL。 Overrides the action attribute of the button's form owner.覆盖按钮的表单所有者的 action 属性。 Does nothing if there is no form owner.如果没有表单所有者,则不执行任何操作。

您可以通过getdocumentbyid(“ id”)。value获取具有ID的值,并使用javascript通过使用像这样的表单ID(“ formid”)。submit()来提交表单。不要忘记删除按钮类型=提交

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

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