2017-07-22 15 views
0

私はフォームを作成しましたが、ボタンを押すだけでフォームが送信されます。Enterキーが押された場合にも、それを送信するにはどうすればいいですか?Enterを押すか送信ボタンを押してフォームを送信するには?

<form method="POST" id="form01"> 
<textarea name="inputBox123" id="myTextarea" style="white-space:nowrap;"> 
</textarea> 
<input type="button" value="Submeter" onclick="myFunction()" /> 
</form> 

<script type="text/javascript"> 

function myFunction(val) { 

var testThis = document.getElementById("myTextarea").value; 

if (testThis.indexOf("launch") > -1) { 
window.location = 'http://www.cateto.weebly.com/benoit.html'; 
return false; 


} 

} 
</script> 

おかげで、

トム代わり<input type="button" />を使用しての

答えて

2

、フォームを送信するために<input type="submit" />を使用します。ここに私のフォームのコードです。

入力ボタンは、デフォルトでフォームを送信する必要があります。

0

あなたはtextareaタグにイベントリスナーを添付しkeypressのために耳を傾け、Enterが押されたとき、あなたはあなたの関数を呼び出す必要があります。

var textarea = document.querySelector("textarea");//get textarea tag 
 
//NOW replace this with: var input = document.getElementById("myTextarea") 
 

 
//BELOW change textarea with input 
 
textarea.addEventListener("keypress", function(e){ 
 
    console.log(e.which, e.target.value); 
 
    if(e.which === 13)//code number for enter key 
 
    myFunction(e.target.value);//run function with value 
 
}); 
 

 

 
function myFunction(val) { 
 

 
var testThis = document.getElementById("myTextarea").value; 
 

 
if (testThis.indexOf("launch") > -1) { 
 
window.location = 'http://www.cateto.weebly.com/benoit.html'; 
 
return false; 
 

 

 
} 
 

 
}
<form method="POST" id="form01"> 
 
<textarea name="inputBox123" id="myTextarea" style="white-space:nowrap;"> 
 
</textarea> 
 
<!-- change above with input in the comment I entered --> 
 
<input type="submit" value="Submeter" onclick="myFunction()" /> 
 
</form>

+0

どうもありがとう!