获取input标签的值
时间: 2023-10-04 18:12:35 浏览: 147
获取input标签的值可以通过JavaScript来实现,具体方法如下:
1. 首先获取input标签的元素对象,可以使用document.getElementById()方法或者document.querySelector()方法。
```javascript
var input = document.getElementById('inputId'); // 通过id获取
// 或者
var input = document.querySelector('input[name="inputName"]'); // 通过name属性获取
```
2. 然后使用input.value属性获取input标签的值。
```javascript
var inputValue = input.value; // 获取input标签的值
```
完整代码示例:
```html
<input id="inputId" type="text" name="inputName" value="hello world" />
<button onclick="getValue()">获取input的值</button>
<script>
function getValue() {
var input = document.getElementById('inputId');
var inputValue = input.value;
alert(inputValue);
}
</script>
```
以上代码会弹出一个对话框,显示input标签的值"hello world"。
阅读全文
相关推荐

















