---
title: "JavaScript检查输入框是否有值再激活提交按钮的方法"
author: "wandering192"
date: "2010-08-15T11:16:30Z"
tags: []
categories: ["编程语言"]
slug: "javascript"
draft: false
lastmod: "2010-08-15T11:16:30Z"
id: 737670
url: "https://wandering192.iteye.com/blog/737670"
---

今天，想做一个“JavaScript检查输入框是否有值再激活提交按钮的方法”的JS简单应用，GOOGLE了一下，也有些文章介绍了方法，自己做了些修改，也写了一个代码：

- 我的JS代码，运行通过：

```
<html>
<head>
		<script>
			function init() {
				test.htmer.disabled = true;
			}
		
			function checkinput(){
				if(test.htmer1.value != '' ){
					test.htmer.disabled = false;
				}
			}
		</script>
</head>
<body onload="init()">

<h1 align="center">JavaScript检查输入框是否有值再激活提交按钮的方法</h1>
	<form action="a.jsp" method="post" name="test" >
		<input name="htmer1" onpropertychange="checkinput();"  maxlength="10" size="12">
		<br>
		<input name="htmer" type="submit" value="提交">
	</form>
	
</body>
</html>
```

- 我google得来的代码：
  
  ```
  <script>
  function checkinput(){
  htmer.disabled = (htmer1.value == '' )
  }
  </script>
  <input name="htmer1" onpropertychange="checkinput();">
  <input name="htmer" type="submit" disabled value="提交">
  ```
- 根据google来的代码，我修改了一下，写了以下代码，运行通过：
  
  ```
  <html>
  	<head>
  	
  		<script>
  			window.onload=init;
  			function init() {
  				test.htmer.disabled = true;
  			}
  		
  			function checkinput(){
  				test.htmer.disabled = (test.htmer1.value == '' )
  			}
  		</script>
  		
  	</head>
  	<body>
  
  		<form method="post" action="" name="test">
  			<input name="htmer1" onpropertychange="checkinput();">
  			<input name="htmer" type="submit" value="提交">
  		</form>
  		
  	</body>
  </html>
  ```