开发者

Keypress event not working

I am using this code but keypress event not working

 <script type="text/javascript">
    $(document).ready(function() {
        $('#txt_tempusername').keypress(function() {

            var href = $('#providerurl').val();
            href = href.toString().replace("{username}", $('#txt_tempusername').val());
            $('#btn_idgo').attr('href', href);

        });
    });

</script>

and this is my HTML

<div class="Input_Div">
 <input type="text" id="txt_tempusername" />
 <a class='example1demo' id="btn_idgo">Go&lt;/a开发者_高级运维>
 <input type="hidden" id="providerurl" />
</div>


Working sample here: http://jsfiddle.net/ezmilhouse/6zfw8/2/

Guess the events worked fine but your 'href' treatment didn't work because the hidden fields value was not defined.

Fixed your code the way I think you wanted it to work:

your html:

<div class="Input_Div">
    <input type="text" id="txt_tempusername" />
    <a class='example1demo' id="btn_idgo">Go!</a>
    <input type="hidden" id="providerurl" value="http://provider-url-{username}.html" />
</div>

your js:

$(document).ready(function() {
    $('#txt_tempusername').keyup(function() {
        var href = $('#providerurl').val().replace("{username}", $(this).val());
        $('#btn_idgo').attr('href', href);
    });
});


Put an

`alert("foo");`

in the function and see if you get a message box. If you don't get one, the element with the ID txt_tempusername does not exist.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜