开发者

Modify what the user typed on a text box onsubmit and select?

I'm using a script to return what the user types on an inputbox without www.

I want the modification to work in the inputbox itself. Is this possible?

Like for example the user types: www.example.com and when he presses GO www. disappears from what he typed and then the text is highlighted (selected).

<input type="text" id="url" />
<input type="button" id="submit" value="Go!" />

<div id="output"></div>

<script>
$("#submit").click(
    function() {
        var url = $("#url").val();

        if(url.match(/^www\./))
        {
            url =开发者_StackOverflow社区 url.substring(4);
        }

        $("#output").html(url);
    }
);
</script>


Yes, $("#url").val(url);

http://jsfiddle.net/hk5zx/

Update: He wants the text selected too!

$("#url").val(url).focus().select();

http://jsfiddle.net/hk5zx/1/


$("#submit").click(
    function() {
        var url = $("#url"),
            val = url.val();

        if(val.match(/^www\./))
        {
            val = val.substring(4);
            url.val(val);
        }

        $("#output").html(val);
    }
);


Yes, set the value through jQuery with $("#url").attr("value", url);.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜