html中怎么写触发文本,在文本框中按Enter时如何触发HTML按钮?

$(document).ready(function(){

$('#TextBoxId').keypress(function(e){

if(e.keyCode==13)

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

});

});

There are a js-free solution.

Set type=submit to the button you'd like to be default and type=button to other buttons. Now in the form below you can hit Enter in any input fields, and the Render button will work (despite the fact it is the second button in the form).

Example:

title='Перейти к редактированию программы'

type=button>

Edit program

title='Построить фрактал'

type=submit

formaction='javascript:alert("Bingo!");'>

Render

Tested in FF24 and Chrome 35 (formaction is html5 feature, but type is not).

It is 2019.

DO NOT USE keypress

keypress event is not triggered when the user presses a key that does not produce any character, such as Tab, Caps Lock, Delete, Backspace, Escape, left & right Shift, function keys(F1 - F12).

The keypress event is fired when a key is pressed down, and that key normally produces a character value. Use input instead.

It has been deprecated.

NOTE | The keypress event is traditionally associated with detecting a character value rather than a physical key, and might not be available on all keys in some configurations.

WARNING | The keypress event type is defined in this specification for reference and completeness, but this specification deprecates the use of this event type. When in editing contexts, authors can subscribe to the beforeinput event instead.

DO NOT USE KeyboardEvent.keyCode

It has been deprecated.

Deprecated | This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

What should I use then? (The good practice)

// Make sure this code gets executed after the DOM is loaded.

document.querySelector("#addLinks").addEventListener("keyup", event => {

if(event.key !== "Enter") return; // Use `.key` instead.

document.querySelector("#linkadd").click(); // Things you want to do.

event.preventDefault(); // No need to `return false;`.

});

Replace the button with a submit

Be progressive, make sure you have a server side version

Bind your JavaScript to the submit handler of the form, not the click handler of the button

Pressing enter in the field will trigger form submission, and the submit handler will fire.

You could add an event handler to your input like so:

document.getElementById('addLinks').οnkeypress=function(e){

if(e.keyCode==13){

document.getElementById('linkadd').click();

}

}

It works when input type="button" is replaced with input type="submit" for the default button which needs to be triggered.

This should do it, I am using jQuery you can write plain javascript.

Replace sendMessage() with your functionality.

$('#addLinks').keypress(function(e) {

if (e.which == 13) {

sendMessage();

e.preventDefault();

}

});

First of all add jquery library file jquery and call it in your html head.

and then Use jquery based code...

$("#id_of_textbox").keyup(function(event){

if(event.keyCode == 13){

$("#id_of_button").click();

}

});

Based on some previous answers, I came up with this:

do not click me

$('#but').click(function(e) {

alert('button press');

e.preventDefault();

});

Take a look at the Fiddle

EDIT:

If you dont want to add additional html elements, you can do this with JS only:

$("input").keyup(function(event) {

if (event.keyCode === 13) {

$("button").click();

}

});

I am using a kendo button. This worked for me.

Search By Customer Name/ Customer Number:

@Html.TextBox("txtSearchString", null, new { style = "width:400px" , autofocus = "autofocus" })

@(Html.Kendo().Button()

.Name("btnSearch")

.HtmlAttributes(new { type = "button", @class = "k-primary" })

.Content("Search")

.Events(ev => ev.Click("onClick")))

var validator = $("#indexform").kendoValidator().data("kendoValidator"),

status = $(".status");

$("#indexform").keyup(function (event) {

if (event.keyCode == 13) {

$("#btnSearch").click();

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值