开发者

Trouble with keyup in jQuery

开发者 https://www.devze.com 2023-02-07 17:37 出处:网络
Maybe noobie question about the keyup function, if the document is ready the div with id \"testdiv\" is empty that should not be. I want the testd开发者_如何学Civ empty if you really keyup. I have mad

Maybe noobie question about the keyup function, if the document is ready the div with id "testdiv" is empty that should not be. I want the testd开发者_如何学Civ empty if you really keyup. I have made a tiny script like this:

<script>
    $(document).ready(function(){  
        $('#test:input').keyup(function () {
            $('#testdiv').empty();              
        }).keyup(); 
    });
</script>
<input type="textfield" id="test" value="test123"/>
<div id="testdiv">Test</div>

Do I have to bind it? Sorry for this beginner question.

Regards,

Frank


$('#test:input').keyup(function () {
    $('#testdiv').empty();              
}).keyup();

This says "bind a keyup handler, then immediately trigger it". The second keyup call triggers the handler. If you don't want it fired immediately, remove it:

$('#test:input').keyup(function () {
    $('#testdiv').empty();              
});


This line:

}).keyup();

...Immediately executes the function you defined for keyup. You don't want that.

$(document).ready(function(){  
    $('#test:input').keyup(function () {
        $('#testdiv').empty();              
    });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消