简体   繁体   English

如何使用 jquery 连续检查输入值?

[英]How to continuously check the value of an input using jquery?

I have an input that is empty after page load, and if a user suddenly fill a value on it the value of the two inputs on the if condition should change to.我有一个在页面加载后为空的输入,如果用户突然在其上填充一个值,则 if 条件下的两个输入的值应更改为。

My problem is when I fill an input, the if condition result doesn't changed in real time.我的问题是当我填写输入时,if 条件结果不会实时更改。

script:脚本:

$(document).ready(function(){
    var countTimerEmailName = setInterval(
            function ()
            {
            emailName();    
            }, 500);        
    function emailName(){

            if($('#emailCodeResult').val() != '' || $('#email').val() == ''){
                clearInterval(countTimerEmailName);
            }

            $.ajax({
            url:"view.php",
            type:"GET",
            data: { term : $('#email').val() },
            dataType:"JSON",
            success: function(result) {


            }

        });

    };
    });

Fire your function on change 激发您的功能

$('#inputA, #inputB').change(function(){
  if($('#inputA').val() != '' || $('#inputB').val() == '') {
   alert("True");
  }else{
    alert("False");
  }
})

You can use keyup like this: 您可以这样使用keyup

$('#textboxID').on( 'keyup', function () {

//write your code here

});

For touch devices as rzr siad keyup won't occur. 对于触摸设备,不会发生rzr siad键盘输入。 You can write a function that is called every, say 1 sec or whatever, like this: 您可以编写一个每隔1秒或诸如此类调用的函数,如下所示:

    t1=window.setInterval(function(){foo()},1000);
function foo()
{
// write function definition here
}

or use blur as others suggested, if the you want to check value only after the user is done with that textbox and moves to next field. 或者,如果您只想在用户使用完该文本框并移至下一个字段后才检查值,则可以使用blur建议。

var fieldcheck;

$(function(){

    $("#input").focus(function(){

        fieldcheck = setInterval(function(){
    
                var innerValue = $("#input").val();
    
                if(innerValue == ""){
    
                    //your code
                    
                } else {
    
                    //your code
                
                }

        }, 100);
    });

     $("#input").blur(function(){
                clearInterval(fieldcheck);        
            });
});

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

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