2017-03-07 42 views
2

我試圖在文本輸入字段中使用「輸入」來提交消息(在SignalR聊天中)。我試過數百種方法,但似乎無法使其工作。mvc - 輸入時提交文本輸入字段

我想要它,所以當我按下輸入它點擊btn或按下標籤,然後輸入。

這裏是輸入和BTN

<input class="form-control" id="message" maxlength="200" /> 

<input type="button" class="btn btn-default" id="sendmessage" value="Send" /> 

回答

2

變化

<input type="button" ... 

<input type="submit" ... 

<button type="submit" ... 

另請參閱button type文檔。

+0

仍然沒有工作:/ – CAMELYON

+0

@CAMELYON - 確保你用[form](https://www.w3schools.com/tags/tag_form.asp)元素包裝你的表單。 – Igor

0

如果你不想使用表單提交,您可以定義在JavaScript中的邏輯,

document.getElementById("message")     // to get the text box 
    .addEventListener("keyup", function(evt) {   // Keyup -> Any key pressed 
    if (evt.keyCode == 13) {       // 13 for enter 
     document.getElementById("sendmessage").click(); 
    } 
    }); 

對於鍵碼,你可以參考this網站