html5 input event,HTML text input event

本文介绍了如何使用JavaScript通过keydown或keyup事件拦截输入,防止文本直接输入,并展示了如何在表单提交前进行值检查。通过jQuery实例演示了禁用提交按钮和验证输入的策略。

In answer to your questions...

The keydown or keypress events can

be intercepted before text is input

using javascript

You can return false after binding a

function to the keydown or keypress

event. This will prevent the text

from being input.

Example using jQuery:

$("#inputfield").live("keydown", function(){

return false;

});

If you want to test some input for value on form submit you can do that using jQuery submit().

$("#theform").submit(function() {

var formval = $("#theinput").val();

if(formval == "") { //or some better test

alert("input value"); //or some other alert...

return false;

} else {

return true;

}

});

Returning false will invalidate the click to submit the form.

Edit:

If as you say in your comments you want to disable the submission of the form until requirements are met for the input field(s)

$("#inputfield").live("keyup", function(){

if($("#inputfield").val() == "") {

$('#button').attr("disabled", true);

} else {

$('#button').attr("disabled", false);

}

});

You want to use the "keyup" event handler to allow the text to be sent first. see my example: on jsbin

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值