简体   繁体   English

javascript中的html输入值

[英]html input values in javascript

I can't seem to figure out this problem that I have: 我似乎无法弄清楚我遇到的这个问题:

<script>
    function getValues(){
        var value1 = document.getElementById("value1");
        var value2 = document.getElementById("value2");
        var value3 = document.getElementById("value3");
        alert(value1 +" "+ value2 +" "+ value3);
    }
</script>

<p>value1</p>   
<input type="text" id='value1' />
<p>value2</p>
<input type="text" id='value2' />
<p>value3</p>
<input type="text" id='value3' />
<input type="button" value="submit" onclick="getValues()" />

I try to get the values the user filled in and then alert them back to them but instead of showing the values they filled in, it says: 我尝试获取用户填写的值,然后将其提醒回他们,但没有显示他们填写的值,而是说:

[object HTMLInputElement] [object HTMLInputElement] [object HTMLInputElement]

Does anyone have an idea what could be the problem? 有谁知道可能是什么问题?

Try it like this 像这样尝试

function getValues(){

        var value1 = document.getElementById("value1").value;
        var value2 = document.getElementById("value2").value;
        var value3 = document.getElementById("value3").value;
        alert(value1 +" "+ value2 +" "+ value3);
    }

var value1 = document.getElementById("value1");

Gets the DOM Element associated with the value1 Id. 获取与value1 Id关联的DOM元素。 In order to get the value of the element you need to do: 为了获得元素的值,您需要执行以下操作:

var value1 = document.getElementById("value1").value; .

document.getElementById("value1") returns a dom element. document.getElementById("value1")返回dom元素。

To assign it, get the value by using it like this: 要分配它,请像下面这样使用它来获取值:

document.getElementById("value1").value

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

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