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
5 Answers
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
版权声明:本文标题:javascript - How to programmatically click on a text input using JQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741462153a2380100.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论