How can I add an unremovable prefix to an HTML input field?

前端 未结 16 1440
猫巷女王i
猫巷女王i 2020-12-13 04:23

Using jQuery, how can I add a default value of http:// into an input field that can’t be removed, but that still allows you to type a URL after it?

Defa

16条回答
  •  北荒
    北荒 (楼主)
    2020-12-13 05:02

    That's not possible. You can put a value in the input field, but it can be deleted.

    You can put the text outside the input field, that will protect it from being deleted, but it won't be included in the value when the form is posted.

    You can use absolute positioning to put the input field on top of the text, and use padding in the field so that you start typing after the text. Example:

    CSS:

    .UrlInput { position: relative; }
    .UrlInput label { position: absolute; left: 3px; top: 3px; color: #999; }
    .UrlInput input { position: absolute; left: 0; top: 0; padding-left:40px; }
    

    HTML:

提交回复
热议问题