如何将表单内容输入为变量? [英] How to input the content of a form as a variable?

查看:144
本文介绍了如何将表单内容输入为变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个使用HTML和JavaScript作为学校项目的通用转换器,并且要将诸如英尺到英里的东西转换为两个单位,您首先必须输入要转换为表格的内容.然后,当点击确认"按钮时,(此时,因为您尚无法转换内容),应该将HTML中的span值更改为已在表单中键入的值.

I'm creating a universal converter using HTML and JavaScript as a school project, and to convert two units of something, such as feet to miles, you first have to type what you want to convert into a form. Then, when the Confirm button is hit, [at this point, because you can't convert things yet] it's supposed to change the value of a span in HTML to what has been typed into the form.

现在,尽管单击按钮,无论将其放在窗体中的是什么,该范围的值始终会更改为"[object HTMLInputElement]".

Right now, though, when the button is clicked, no matter what is put into the form, the span's value always gets changed to "[object HTMLInputElement]".

HTML和JavaScript在下面.

The HTML and JavaScript are below.

HTML:

 <form>
    <input type="text" id="unit">
</form>
<br>
    <button class="btn btn-sm btn-danger" id="confirm">Confirm</button>
<br>
<br>
    <h4>Pick two units of <span id="unitType">[?]</span> you want to convert.</h4>

JavaScript:

JavaScript:

document.getElementById("confirm").onclick = function() {
    var unitInput = document.getElementById("unit");
    var unitType = document.getElementById("unitType");
    unitType.innerHTML = unitInput;
};

谢谢.

推荐答案

使用document.getElementById('unit');时,实际上并没有获得文本输入的值.那只会得到 element .您需要像这样获取文本框的值:

You are not actually getting the value of the text input when you use document.getElementById('unit');. That only gets the element. You need to get the value of the textbox like this:

var unitInput = document.getElementById('unit').value;

这将获得文本框的值.

参考文献: MDN

References: MDN

这篇关于如何将表单内容输入为变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆