简体   繁体   English

如何从表单获取值并防止提交?

[英]How to get the values from a form and prevent submission?

I have this simple form: 我有这个简单的形式:

HTML 的HTML

<form>
    <label for="eName">Name</label>
    <input id="eName" type="text" name="eName">
    <label for="Email">Email</label>
    <input id="Email" type="text"  name="Email">
    <button id="create" class="boton" 
        onclick="doSomething();" type="submit">Create!</button>
</form>

JS JS

function doSomething() {
    var name, email;
    name = document.getElementById("eName").value;
    email = document.getElementById("Email").value;
    putElementsIntoTheDOM(name, email);
}

When the user inputs some information I want to populate the DOM with the user input. 当用户输入一些信息时,我想用用户输入来填充DOM。 The example above works. 上面的示例有效。 But I think it can be done better. 但我认为可以做得更好。 I just don't know how. 我就是不知道

How can I wire the <button> so that when the user clicks it the form values are passed to the function doSomething() ? 如何连接<button>以便当用户单击它时将表单值传递给函数doSomething()

Also, since I'm not sending the form values anywhere except populating the DOM, how can I prevent the submission? 另外,由于除了填充DOM外,我不会在其他任何地方发送表单值,因此如何防止提交?

I've seen something like this but I can't get it too work. 我已经看到过类似的内容,但是我无法正常工作。

<button id="create" class="boton" onclick="doSomething(this.form);"     
    type="submit">Create!</button>

First of all you have to update your function declaration to be able to receive the variables you want to send 首先,您必须更新函数声明以能够接收要发送的变量

function doSomething(name,email) {
}

Secondly, if you have to send values of some fields to that function, you can do so on button click like this. 其次,如果必须将某些字段的值发送到该函数,则可以像这样单击按钮。

<button id="create" class="boton" onclick="doSomething(document.getElementById('eName').value,document.getElementById('Email').value);" type="submit">Create!</button>

However, using unobtrusive javascript is recommended, and for that jQuery is one of the options you can use for passing variables to your function neatly. 但是,建议使用不引人注意的javascript,为此,jQuery是可用于将变量整洁地传递给函数的选项之一。

If you don't want to send the form values anywhere, then you just need to remove type="submit" from your button. 如果您不想在任何地方发送表单值,则只需从按钮中删除type="submit"

Your example code works fine. 您的示例代码可以正常工作。 I'm not sure what you mean by a 'better' way. 我不确定“更好”的意思。 More modern/idiomatic javascript would not be using the onclick attribute, but instead binding doSomething to the button. 更现代/更惯用的javascript不会使用onclick属性,而是将doSomething绑定到按钮。 Using jQuery, that would look like: 使用jQuery,它看起来像:

$("#create").click(doSomething);

There is a difference between the type="submit" and type="button" that I didn't realize. 我没有意识到type="submit"type="button"之间的区别。
Also, the button and submit types react differently with onclick and onsubmit events. 同样, buttonsubmit类型对onclickonsubmit事件的反应也不同。

For example 例如

<form onclick="doSomething()">
<label for="eName">Name</label>
<input id="eName" type="text" name="eName">
<label for="Email">Email</label>
<input id="Email" type="text"  name="Email">
<button id="create" class="boton" type="button">Create!</button>
</form>

Notice that at the top of the form there is onclick . 请注意, form顶部是onclick
The onclick is fired whenever you focus on an input element, and of course if you click the button. 只要您专注于输入元素,当然也可以单击按钮,就会触发onclick

Changing the form to <form onsubmit="doSomething(); but not changing the type="button" doesn't do anything. Clicking the button doesn't trigger the function. 将表单更改为<form onsubmit="doSomething();但不更改type="button"不会执行任何操作。单击按钮不会触发该函数。

By changing the type="submit" and keeping the head <form onsubmit="doSomething(); triggers the function when the button is clicked. A nice added functionality to this is that if you have any <input ... required="required"> the submit will only work if those fields are filled in (and your form will let you know about the required fields). 通过更改type="submit"并保持头部<form onsubmit="doSomething();在单击按钮时触发该函数。一个很好的附加功能是,如果您有任何<input ... required="required">submit如果这些字段是在充满(和你的表格将让您了解所需的字段)才有效。

To prevent the submission/refreshing (since I'm only populating the DOM with user input) adding return false at the form head prevents submission 为了防止提交/刷新(因为我只用用户输入填充DOM),在表单头添加return false可以防止提交
<form onsubmit="doSomething(); return false"> . <form onsubmit="doSomething(); return false">

Finally, to get the form values adding this : 最后,要获得添加this的表单值:
<form onsubmit="doSomething(this); return false> and then <form onsubmit="doSomething(this); return false> ,然后

function doSommething(formInfo) {
   var name = formInfo.eName.value;
   var email = formInfo.Email.value;
   ... 
 }

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

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