开发者

Trouble with keyup in jQuery

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜