Javascript函数未定义onclick

时间:2017-05-27 22:30:14

标签: html5 function onclick

<head>
	<script language='javascript' type='text/javascript'>
		function insertTemplate() {
			console.log('something is coming out');

			if (document.getElementById('template').checked) {
				var test = "lore ipsum";
				document.getElementById('message') = test;
				alert(test);
			}
			else (
				var nothing = '';
				document.getElementById('message') = nothing;
			)
		}
	</script>
</head>
<body>
	<form  class='form-horizontal' role='form' method='post'>
		{{ csrf_field() }}
		<input type='text' name='address'><br>
		<input type='text' name='subject'><br>
		<input type='checkbox' name='template' id='template' onclick = "insertTemplate()"> Insert in template</br>
		<input type='text' name='message' id = 'message' style='width:500px;height:500px;'><br>
		<button type='submit'>Submit</button>
	</form>
</body>
我正在使用Laravel创建一个应用程序。我试图在用户单击复选框时使用文本填充文本字段。但是,每当我选中该复选框时,我都会继续将“未捕获的ReferenceError:insertTemplate未定义”视为错误。我似乎已经在输入的“onclick”属性上声明了该函数,并定义了该函数。我不知道我做错了什么。

1 个答案:

答案 0 :(得分:1)

您的else块由括号而不是花括号分隔。

	<form  class='form-horizontal' role='form' method='post'>
		{{ csrf_field() }}
		<input type='text' name='address'><br>
		<input type='text' name='subject'><br>
		<input type='checkbox' name='template' id='template' onclick = "insertTemplate()"> Insert in template<br>
		<input type='text' name='message' id = 'message' style='width:500px;height:500px;'><br>
		<button type='submit'>Submit</button>
	</form>
  
  	<script>
		function insertTemplate() {
			console.log('something is coming out');
      
      var test = "lore ipsum"
      var nothing = "";

			if (document.getElementById('template').checked) {
				document.getElementById('message').textContent = test;
				alert(test);
			} else {
				document.getElementById('message').textContent = nothing;
			}
		}
	</script>