2017-05-27 131 views
0

JavaScript函數的onclick

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

 
\t \t \t if (document.getElementById('template').checked) { 
 
\t \t \t \t var test = "lore ipsum"; 
 
\t \t \t \t document.getElementById('message') = test; 
 
\t \t \t \t alert(test); 
 
\t \t \t } 
 
\t \t \t else (
 
\t \t \t \t var nothing = ''; 
 
\t \t \t \t document.getElementById('message') = nothing; 
 
\t \t \t) 
 
\t \t } 
 
\t </script> 
 
</head>
<body> 
 
\t <form class='form-horizontal' role='form' method='post'> 
 
\t \t {{ csrf_field() }} 
 
\t \t <input type='text' name='address'><br> 
 
\t \t <input type='text' name='subject'><br> 
 
\t \t <input type='checkbox' name='template' id='template' onclick = "insertTemplate()"> Insert in template</br> 
 
\t \t <input type='text' name='message' id = 'message' style='width:500px;height:500px;'><br> 
 
\t \t <button type='submit'>Submit</button> 
 
\t </form> 
 
</body>
我創建使用Laravel的應用程序。我試圖在用戶點擊複選框時用文本填充文本字段。但是,每當選中該框時,我都會繼續收到「Uncaught ReferenceError:insertTemplate is not defined」作爲錯誤。我似乎已經說明了輸入的「onclick」屬性上的功能並定義了該功能。我不知道我做錯了什麼。

+0

別人或許應該用花括號寫 –

回答

1

您的else塊被括號括起來,而不是花括號。

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

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