简体   繁体   English

输入时清除输入框:使用javascript在html中无效

[英]Clear input box when input:invalid in html using javascript

My goal is to clear an input type text box when they are focused ONLY IF the input is INVALID. 我的目标是只在输入为INVALID时才清除输入类型文本框。 Let's say i have a text box that has a maximum value of 200 假设我有一个最大值为200的文本框

<input id="textbox" type = "text" max="200" runat="server">

so let's say I input 150 so it's valid hence if I focus again on it, it will remain its value 150 but, if i input greater than 200, my text box will have have a red border color on it and focused on another textbox(its working on my current code) and if i will focus on it again, i want to clear its value. 所以让我说我输入150因此它是有效的,因此如果我再次关注它,它将保持其值150但是,如果我输入大于200,我的文本框将有一个红色边框颜色,并专注于另一个文本框(它正在处理我当前的代码)如果我再次关注它,我想清除它的价值。

I wanna do something like this. 我想做这样的事情。

 input:invalid {
        box-shadow: 0 0 1px 3px #e74c3c;
    }

    input:focus:invalid {
        textbox.value = "" (i know this will not work,
                            but i wanna do something like this but how?)
    }

Note that I have multiple textboxes with different max and min values, not just one. 请注意,我有多个具有不同最大值和最小值的文本框,而不仅仅是一个。

Please Try this pure JavaScript: 请试试这个纯JavaScript:

 function myFunction(control,max,min){ var val= Number(control.value); if(val>max || val<min){ control.value=""; } } 
 <input id="textbox" type = "text" max="200" onFocus="myFunction(this,250,100)"> 

Let assume your max value is 250 and min value is 100 假设您的最大值为250,最小值为100

You can use ValidityState interface which enables you to check the state of each input field. 您可以使用ValidityState接口,它可以检查每个输入字段的状态。 First you need to listen onfocus event. 首先,你需要听取onfocus事件。 For example: 例如:

var form = document.querySelector('form'),
    elements = form.querySelectorAll('input'), 
    i;

for (i = 0; i < elements.length; i++) {
    elements[i].addEventListener('focus', function() {
        if (!this.validity.valid) {
            this.value = '';
        }
    }, false);
}

Demo: http://jsfiddle.net/yrovm6uh/ 演示: http//jsfiddle.net/yrovm6uh/

I think this is what your after, it changes red once it reaches the max amount of characters which is taken from the value in the input 我认为这是你的后续,一旦达到从input的值中取出的max字符数,它就会变红

At that point it also focuses on a seperate textbox with the cursor in it ready for the user to continue typing which uses its own max character value before turning red it self. 此时它还专注于一个单独的textbox ,其中有光标,用户可以继续键入,然后使用自己的max字符值,然后再将其变为红色。

And if a user then clicks on the first textbox that turned red it will be cleared and the code below has the ability to clear the second textbox at the same time. 如果用户点击第一个变为红色的textbox ,它将被清除,下面的代码可以同时清除第二个textbox

$(document).ready(function(){
    $(":input").on("focus keyup", function(){
        var max = $(this).attr("max");
        var current = $(this).val().length;

        if(current < max){
            $(this).css({"border" : "1px solid blue", "outline" : "none"});
        }else{
            $(this).css({"border" : "1px solid red", "outline" : "none"});
            $(this).focus(function(){
                $(this).val(" ").focus();
                //add this if you want the overflow textbox to be cleared aswell
                //$("#overflow").val(" ").css({"border" : "", "outline" : "none"});
            });
            $("#overflow").focus(); 
        }
    });
});

you can see it in action HERE 你可以看到在行动它HERE

Hope this helps 希望这可以帮助

using java script: 使用java脚本:

<input id="textbox" type = "text" max="200" onFocus="validate(this)">

JS: JS:

function validate(control){
    var val= parseInt(control.value);

    if(val>200){
        control.value="";
    }
}

you can use jquery and bind focus like 你可以使用jquery和绑定焦点

$('#textbox').bind('focus', function() {

    if ( $('#textbox').val().length>200 ) { 

        $('#textbox').val('');

    }

});

can bind keyup event as well, it'll clear the textbox the moment user types more than 200 like. 也可以绑定keyup事件,它会在用户输入200以上时清除文本框。

$('#textbox').bind('keyup', function(e) {

    if ( $('#textbox').val().length>200 ) { 

        $('#textbox').val('');

    }

});

fiddle : http://jsfiddle.net/invincibleJai/Laqeh79u/ 小提琴: http//jsfiddle.net/invincibleJai/Laqeh79u/

I'd suggest: 我建议:

$('input').on('invalid', function () {
    this.value = '';
});

References: 参考文献:

<input id="textbox" type = "text" max="200" runat="server">

in Jquery : 在Jquery:

$("#textbox-id").focus(function(){
if($(this).val() > 200)
{
  $(this).val("");
  $(this).css("border","1px solid red");
}

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM