admin管理员组

文章数量:1289542

I'm trying to implement a messaging application in my game, so instead of clicking on the input text field manually, I want users to only press "enter", write something, then press "enter" again to submit.

For some reason, when I do this (press "enter"), the onclick alert fires from the input, but the input stays the same, I am still not able to type into the input form. If I manually click it, it works fine.

Am I missing something?

HTML

<form id="messageInput" action="">
    <input id="m" autoplete="off" maxlength="100" onclick="alert('clicked')"/>
</form>

JAVASCRIPT

if(keyOn["enter"]){
    keyOn["enter"] = false;

    $('#m').click();

    console.log("clicked");
}

I'm trying to implement a messaging application in my game, so instead of clicking on the input text field manually, I want users to only press "enter", write something, then press "enter" again to submit.

For some reason, when I do this (press "enter"), the onclick alert fires from the input, but the input stays the same, I am still not able to type into the input form. If I manually click it, it works fine.

Am I missing something?

HTML

<form id="messageInput" action="">
    <input id="m" autoplete="off" maxlength="100" onclick="alert('clicked')"/>
</form>

JAVASCRIPT

if(keyOn["enter"]){
    keyOn["enter"] = false;

    $('#m').click();

    console.log("clicked");
}
Share Improve this question edited Jan 21, 2017 at 3:16 tery.blargh asked Jan 21, 2017 at 3:12 tery.blarghtery.blargh 4052 gold badges6 silver badges23 bronze badges 3
  • 1 i am removing jquery tag in here as you wanted pure js on this – guradio Commented Jan 21, 2017 at 3:13
  • Jquery is preferable, I should include in title – tery.blargh Commented Jan 21, 2017 at 3:15
  • 1 @tery.blargh check the solution for you. – Hikmat Sijapati Commented Jan 21, 2017 at 3:35
Add a ment  | 

5 Answers 5

Reset to default 3

$('#m').click(function() {

  alert("click")
}).click();//click here to click automatically on load
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="messageInput" action="">
  <input id="m" autoplete="off" maxlength="100" />
</form>

Using jquery you can call .click()

Thank you all for your help, but I somehow found the solution by randomly checking everything I saw:

$("#m").trigger("focus");

Use focus instead of click

The code you've written is for capturing the click event ($("#m").click()).

Try:

$("#m").trigger("click");

Try like this..press enter.Event will trigger.

if(confirm('Are you want submit message?')){
$("#m").keyup(function(event){
    if(event.keyCode == 13){
       alert('clicked');
       $("#m").val('');
    }
});
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="messageInput" action="">
  <input id="m" autoplete="off" maxlength="100" />
</form>

I was having the same issue in an Angular app where the "click" would fire and open something from the element but it wouldn't do any of the angular method calls I had defined in (click)="...", I also found it wouldn't fire off anything defined in onclick()

The solution for me was to add .get(0)

$(el).get(0).click();

本文标签: javascriptHow to programmatically click on a text input using JQueryStack Overflow