如何删除按键事件中键入的字符 [英] how to delete the character typed in key press event

查看:62
本文介绍了如何删除按键事件中键入的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果它不是按键事件中的数字或小数点,我想删除在文本框中键入的字符。但我不能在处理按键事件的代码中执行此操作,因为在按键事件后显示该字符。请告诉我怎么做?

当输入一个字符时,如果它不是数字,我想删除它,因为如果它没有被删除,如果用户没有删除它,那将导致不正确的数据。

我想只显示数字和一个小数点。如果用户键入任何其他字符,则不应在文本框中显示。



I want to delete the character typed in the text box if it is not a number or decimal point in key press event. but I can not do this in the code that handle key press event because the character is displayed after key press event. please tell me how can I do this ?
when a character is typed, if it is other than number, I want to delete it, because if it is not deleted, if user does not delete it, that will lead to incorrect data.
I want to display only numbers and one decimal point. if user types any other character it should not displayed in the textbox.

span = document.createElement("span");
var txt1=($('<input>').attr('type', 'text').attr('id', 'id1').appendTo(span));
txt1.bind("keypress", function () {
    if (!validatedText(txt1.attr('id'), event)){
         removeCharEntered(txt1.attr('id'));
    }
});
var validatedText = function (id, event) {
var text=$('#' + id).val();
var len = text.length;
var ch = event.charCode;
//check for number
if (ch != 46) {
    if ((ch < 48) || (ch > 57)) {
        alert('please enter numbers only.');
        return false;
    }
}
//check for more than one decimal
else
    for (var i = 0; i < len; i++) {
        if (text.charAt(i) == '.'){
            alert('please enter numbers only.');
            return false;
        }
    }
return true;
};
//remove if character is not a number and decimal 
var removeCharEntered = function (id) {
var text = $('#' + id).val();
var len = text.length;
$('#' + id).val(text.substr(0,len - 1));
}

推荐答案

' < input>')。attr(' type'' text')。attr(' id'' id1')。 appendTo(跨度));
txt1.bind( keypress函数(){
if (!validatedText(txt1.attr('' id'),event)){
removeCharEntered(txt1.attr(' id'));
}
});
var validatedText = function (id,event){
var text =
('<input>').attr('type', 'text').attr('id', 'id1').appendTo(span)); txt1.bind("keypress", function () { if (!validatedText(txt1.attr('id'), event)){ removeCharEntered(txt1.attr('id')); } }); var validatedText = function (id, event) { var text=


' #' + id).val();
var len = text.length;
var ch = event.charCode;
// 检查号码
if (ch!= 46 ){
if ((ch< ; 48 )||(ch> 57 )){
alert(' 请仅输入数字。');
return false ;
}
}
// 检查多个小数
else
for var i = 0 ; i< len; i ++){
if ( text.charAt(i)== ' 。'){
alert(' 请仅输入数字。');
return false ;
}
}
返回 true ;
};
// 如果字符不是数字则删除<十进制
var removeCharEntered = function (id){
var text =
('#' + id).val(); var len = text.length; var ch = event.charCode; //check for number if (ch != 46) { if ((ch < 48) || (ch > 57)) { alert('please enter numbers only.'); return false; } } //check for more than one decimal else for (var i = 0; i < len; i++) { if (text.charAt(i) == '.'){ alert('please enter numbers only.'); return false; } } return true; }; //remove if character is not a number and decimal var removeCharEntered = function (id) { var text =


' #' + id) .VAL();
var len = text.length;
('#' + id).val(); var len = text.length;


这篇关于如何删除按键事件中键入的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆