简体   繁体   English

我应该使用什么按钮事件来确认 html 表单?

[英]What button event should I use to confirm a html form?

Using Javascript, no framework, what button event should I use to confirm form when I wish not redirect?使用Javascript,没有框架,当我不想重定向时,我应该使用什么按钮事件来确认表单? I expect to use either left mouse button or keyboard to confirm the form.我希望使用鼠标左键或键盘来确认表单。

I used this element:我使用了这个元素:

<button type="button" value="1">Save</button>

Using type="submit" with "submit" event is no solution for me because this creates redirection (values the from are lost).将 type="submit" 与 "submit" 事件一起使用对我来说不是解决方案,因为这会创建重定向( from 的值丢失)。 So I use type "button".所以我使用类型“按钮”。

When I use当我使用

document.getElementById("advanced_form").addEventListener("click", saveOptions);

This even "click" is used with mouse.这甚至“点击”与鼠标一起使用。 But there is possibility that the user will use keyboard instead mouse to submit form.但是用户可能会使用键盘而不是鼠标来提交表单。 So I suspect the form would not react to keyboard confirm action.所以我怀疑表单不会对键盘确认操作做出反应。 I did not find any event related to button being pressed.我没有发现任何与按下按钮相关的事件。 So how to solve this problem?那么如何解决这个问题呢?

You could still use ´type="submit"´ in combination with ´e.preventDefault();´ to aviod the redirect.您仍然可以将 ´type="submit"´ 与 ´e.preventDefault();´ 结合使用来避免重定向。

I hope this helped, good luck.我希望这有帮助,祝你好运。

From your clarifying comment:从您的澄清评论中:

What happened is when I clicked the button the values which were in the form disapeared and so I understood it that the form was reloaded without any values.发生的事情是,当我单击按钮时,表单中的值消失了,所以我明白表单被重新加载而没有任何值。

Submitting a form...submits the form.提交表单...提交表单。 What you get back as a result depends entirely on what the server sends back.您返回的结果完全取决于服务器发回的内容。

But the values should not disappear, the behaviour which I need is like in a normal Browser Window (WINAPI)但是值不应该消失,我需要的行为就像在普通浏览器窗口(WINAPI)中一样

That is normal.正常的。

...the page will not clear the values. ...页面不会清除值。 If I'd want to close the form, I'd close the tab (html page).如果我想关闭表单,我会关闭选项卡(html 页面)。

That isn't normal.正常。 Normal is for the form to go away and be replaced by the result of submitting it.正常是表单消失并被提交的结果所取代。

But you can do that with the submit event, just use event.preventDefault() within the submit event to prevent the form submission:但你可以做到这一点的submit事件,只需使用event.preventDefault()的范围内submit事件,防止表单提交:

document.getElementById("the-form").addEventListener("submit", function(event) {
    event.preventDefault(); // Prevents the form from being submitted
});

That event will reliably fire whether the user used the mouse, keyboard, or assistive technology to submit the form.无论用户使用鼠标、键盘还是辅助技术提交表单,该事件都将可靠地触发。 The click event on a submit button will not reliably fire when the form is submitted with the keyboard or assistive technology.当使用键盘或辅助技术提交表单时,提交按钮上的click事件不会可靠地触发。

You can use submit button, but you need to handle submit action.您可以使用提交按钮,但您需要处理提交操作。 Try this (with jquery):试试这个(使用jquery):

<input class="submit_button" type="submit" value="Save" />
<script>
function submit_form(e)
{
    if (check_form_submit()) {
        // check your data here

        $(this).submit();
        return;
    }
    e.preventDefault();
}
$(function() {
    $('.submit_button').parents('form').submit(submit_form);
});
</script>

You should still use the submit input type but you need to prevent the default action so that the page doesn't reload.您仍然应该使用提交输入类型,但您需要阻止默认操作,以便页面不会重新加载。 If you are using jQuery this snippet should help.如果您正在使用 jQuery,这个片段应该会有所帮助。

$('#my-form').submit(function(ev) {
ev.preventDefault(); // to stop the form from submitting
// Your code here
this.submit(); // If you want to submit at the end
});

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

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