-->

ajax call on keyup()

2019-07-25 00:07发布

问题:

I have an issue: if someone is typing in field, this should make the text field for all other user as readonly and show msg like "someone is typing right now".

<div id="msg"></div>
<input type="text" name="field" id="field" />

$("#field").keyup(function (){
    var isTyping = $('#field').val();
    var data = 'result=' + isTyping;
    var msg = $('#msg');
    $.ajax({
        type: 'POST',
        url: "includes/control.php",
        data: data,
        cache: false,
        success: function(){  
           msg.html(html);
        }
    });
});

And this is control.php:

if($_POST['result']){
    echo "someone is typing";
}

This work like a sharm for click() method but not for keyup().

Thanks for any help.

回答1:

Strange that you are sending an ajax request whenever user types but change:

success: function(){  
    msg.html(html);
}

to

success: function(html){  
    msg.html(html);
}

since your html wasn't defined as ajax response.