最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to continuously check the value of an input using jquery? - Stack Overflow

programmeradmin0浏览0评论

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.

My problem is when I fill an input, the if condition result doesn't changed in real time.

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) {


            }

        });

    };
    });

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.

My problem is when I fill an input, the if condition result doesn't changed in real time.

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) {


            }

        });

    };
    });
Share Improve this question edited Jul 27, 2014 at 17:38 asked Jul 27, 2014 at 16:52 user3869809user3869809 3
  • u can use onkeyup event – user63762453 Commented Jul 27, 2014 at 16:54
  • afaik okeyup event wont fire on touch devices – MayThrow Commented Jul 27, 2014 at 16:55
  • In that case use a function that is called after say every 1sec and checks if the value has changed – user63762453 Commented Jul 27, 2014 at 16:57
Add a ment  | 

3 Answers 3

Reset to default 3

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:

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

//write your code here

});

For touch devices as rzr siad keyup won't occur. You can write a function that is called every, say 1 sec or whatever, like this:

    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.

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);        
  });
});
发布评论

评论列表(0)

  1. 暂无评论