简体   繁体   中英

Call function onclick submit button on instant message javascript app

This is the current progress I have done on this instant message web

 <input name="chang" id="textInput" class="input responsive-column" placeholder="Please enter your message here" type="text" onkeydown="ConversationPanel.inputKeyDown(event, this)"> 

Which will send the message dynamically when I press the Enter Key on the keyboard, but I want to also make a button which will do the same function as well, I have already tried to put the input and a submit button in a form but it will refresh the whole page.

  <input type="submit" value="send" onclick=""/> 

Any suggestion for me? Thanks.

kind regards

Change the type with button instead of submit

 <input type="button" value="send" onclick="$('#textInput').trigger('keydown')"/>

OR

use preventDefault() function for preventing the page refresh

<input type="submit" value="send" onclick="event.preventDefault(),$('#textInput').trigger('keydown')"/>

You can try something like onclick="$('#textInput').trigger('keydown')"

It will trigger the keydown function you have in your textInput element

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input name="chang" id="textInput" class="input responsive-column" placeholder="Please enter your message here" type="text" onkeydown="console.log('textInput is triggered')"> <input type="button" value="send" onclick="$('#textInput').trigger('keydown')" /> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM