开发者

Is it possible to put read-only text inside input alongside writable text?

开发者 https://www.devze.com 2023-03-31 17:48 出处:网络
I want to make an input with some read-only text and some writable text. It should always say \"www.\" at the beginning (read only) and afterwards the user should be able to write wh开发者_开发问答ate

I want to make an input with some read-only text and some writable text. It should always say "www." at the beginning (read only) and afterwards the user should be able to write wh开发者_开发问答atever he wants. What is the easiest way to do this?


The "easiest" way would be to have a separate element underneath (or surrounding) the input.

http://jsfiddle.net/NxWBT/

However, this'll be for presentational purposes only. If you need the www part submitted, you'll have to add it with Javascript before the form is submitted.


On KeyDown event you can get the string an append it

function onblurwww(){
    var tb = document.getElementById('myTextBox');
    var val = tb.value;

    if (val.substr(0, 4) != 'www.')
        tb.value = 'www.' + val;
}

something like that in your keydown on the textbox.

0

精彩评论

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